This suddenly stopped working, when trying to instantiate the projectile is tells me the projectile is null. Even though i declare a value to it. The Debug tells me Start() is run. Can't give much more context.
The question here is not "what does this error mean" it's "why is it null?" I got a great reply from Daedalus who told me to stay away from string lookups. I will try that and tell y'all if that worked.
EDIT:// My teacher added some code in the text that i forgot to remove (he couldn't find out why i was getting null) it is however removed now and this is the correct code that does not work. The tag is not miss-spelled, I have checked the casing etc.
void Start()
{
targPlayer = GameObject.FindGameObjectWithTag("Player").transform;
projectile = GameObject.Find("Projectile");
Debug.Log("was run");
}
void Update()
{
fire -= Time.deltaTime;
dmg -= Time.deltaTime;
if (fire <= 0)
{
if (Vector2.Distance(transform.position, targPlayer.position) <= detectionRange)
{
// This is where the error happens
Instantiate(projectile, transform.position, Quaternion.identity);
}
fire = fireRate;
}
if (Vector2.Distance(targPlayer.position, transform.position) <= detectionRange && Vector2.Distance(targPlayer.position, transform.position) >= stopRange)
{
transform.position = Vector2.MoveTowards(transform.position, targPlayer.position, movSpeed * Time.deltaTime);
}
}