I'm trying to make a pick up weapon system on unity using c#. I'm trying to change a value from a script to another but I'm having some problems. I'm trying to change the weapon number to 1.
using UnityEngine;
public class Pew : MonoBehaviour
{
public int weapon = 0;
}
So, I'm using this line of code
using UnityEngine;
public class PickUpBow : MonoBehaviour
{
public void OnCollisionEnter2D(Collision2D collision)
{
GameObject thePlayer = GameObject.Find("ThePlayer");
Pew pew = thePlayer.GetComponent<Pew>();
pew.weapon = 1;
}
}
But when I touch the object, it gives me the following error: "Object reference not set to an instance of an object Unity", on the following line: Pew pew = thePlayer.GetComponent<Pew>();
Thank you!