- Home /
Question by
prof3ta · Jun 12, 2014 at 01:55 AM ·
gameobjectfunctionreferencenull
Null reference when calling a method from another script
So I have a script EnemySpawner. Im trying to call a function there everytime an enemy is destroyed, which happens in my projectile script. I keep getting this:
NullReferenceException: Object reference not set to an instance of an object Projectile.OnTriggerEnter (UnityEngine.Collider otherObject) (at Assets/Scripts/Projectile.cs:57)
Heres what Im basically doing in my projectile script:
public EnemySpawner enemyspawner;
void OnTriggerEnter(Collider otherObject)
{
//do stuff
Destroy(otherobject.GameObject);
//call function
enemyspawner.test();
//i have also tried this
//enemyspawner.GetComponent<EnemySpawner>().test();
//and
//EnemySpawner enen= new EnemySpawner();
//enen.test();
}
heres what im doing in my enemyspawner script
public class EnemySpawner : MonoBehaviour {
public void test()
{
Debug.Log ("CALLED");
}
}
What am I missing here???
Comment
Answer by zharik86 · Jun 12, 2014 at 05:58 AM
Look at the Destroy function. Change it on:
public EnemySpawner enemyspawner;
void OnTriggerEnter(Collider otherObject) {
//do stuff
Destroy(otherObject.gameObject); //You write otherobject.GameObject, change it
//Further for function invocation from other script we shall find at first it through GetComponent or using public variable
enemyspawner.test();
}