- Home /
Disable a script on another game object
I know there's a lot of questions asking this question but the answers on the other questions don't seem to have any luck with me. This is what I have:
public GameObject missile;
void OnTriggerEnter(Collider collide)
{
if(collide.gameObject.name == "powerup2")
{
Debug.Log("hit");
//EnemyAI.speed = 2;
missile.GetComponent<homingMissilePlayer>().enabled = true;
Destroy(collide.gameObject);
StartCoroutine(enemySpeed());
}
}
It's giving me this error though:
NullReferenceException: Object reference not set to an instance of an object
The homingMissilePlayer script is on one of my missiles that is instantiated every few seconds, but I have dragged the prefab of the missile onto the object this script is on.
Can someone please tell me what I'm doing wrong please?
you didn't mention the null reference exception is for which line of your code
Its line 108, which would be line 10 in my above snippet
Answer by SnotE101 · Sep 19, 2014 at 07:20 PM
Is the variable missile, identified in the inspector? If it is then try to use missile = gameobject.find("missile name"); in the beginning of you script.
Yeah it is identified in the inspector, thing is, it's a prefab which gets instantiated every second.
Answer by dmg0600 · Sep 19, 2014 at 08:37 PM
If you are passing a prefab to the variable missile you are enabling the component of that prefab, not its instances.
Unity giving you a NullReferenceException for that line can only mean two things. First, you have not assigned the variable missile neither in the editor nor in the code. Second, the prefab has no homingMissilePlayer component.
Your answer
Follow this Question
Related Questions
Disabling a script . 1 Answer
Enabling/Disabling Multiple Tagged objects 1 Answer
enable/disable specific components 3 Answers
Disable the part of the gameobject 2 Answers