- Home /
Trigger Explosion null reference problem
im new to unity and trying to trigger an explosion.
eg. apartment building entrance. car on fire across the street. player walks through apartment door door, BOOM! car explodes...
oooo cinematic right!? hahaha...
SO! i have a cube with a detonator attached named "Exploding Cube"... then a larger trigger cube named "Explosion Trigger"
"Explosion Trigger" has a JS script with this: and "Exploding Cube" is plugged into the gameobject slot...
var cube : GameObject;
function OnTriggerEnter(collision : Collider) {
cube = GameObject.Find("Exploding Cube");
cube.GetComponent("Detonator").Explode();
cube.GetComponent("MeshRender").enabled = false;
}
i run into the trigger explosion pauses midway and gives me "NullReferenceException: Object reference not set to an instance of an object
HELP PLEASE!!!!!! i wish unity had a visual editor like UDK!!!
Answer by DaveA · Nov 12, 2011 at 02:11 AM
You have a debugger, you can use that. It should stop right on the problem and you can inspect things to see if they are what you expect. Some tips: You don't need to "Quote" the component names. You might want to check collision.name == "ExplodingCube" rather than Find because that's a time-consuming call. You can get null exceptions when things don't exist, so if the Find failed, or there was no Detonator on the cube, or not MeshRenderer on the cube, it would cause this. Also you are setting cube in the Inspector? and with Find? Are you sure you want to do both?