- Home /
Unity Freezing On Successful Detonator Explosion
For some reason, when this script works as intended (Makes "Detonator" explode from the explosion framework plugin on contact with another object), Unity freezes. This is the script I used:
function OnTriggerEnter (other : Collider) {
gameObject.GetComponent("Detonator").Explode();
yield WaitForSeconds(2);
Destroy(this.gameObject);
}
The object that I have this script attached to does not have a detonator on it at all, but it still makes the explosion beforeit freezes. Removing the "gameObject.GetComponent("Detonator").Explode();" from the script fixes it, but I need the explosion to happen.
Do you need the object that has the code above to explode, or do you want the colliding object to explode?
I want the explosion to emit from the object that has this script (It is a missile).
Then why doesn't it have the Detonator script? It seems like that's the script making an object explode.
Having a detonator or not seems to have no effect on what happens. I think that when I call the component "Detonator", it pulls "Explode" from the script in my assets, not the detonator on it.
If your detonator script is derived from monobehavior than it's highly unlikable. Did you try my answer?
Answer by XMatrix2X · Nov 15, 2013 at 12:26 AM
I don't know why but having the original script and disabling the detonators (Checkboxes next to their name in the inspector)causes it to work flawlessly. Thank you for helping!
Answer by gajdot · Nov 14, 2013 at 03:03 PM
You need to check first if you have a detonator, and only if it does do the rest of the code. something like:
Detonator detonator = gameObject.GetComponent("Detonator");
if(detonator!=null){
detonator.Explode();
yield WaitForSeconds(2);
Destroy(this.gameObject);
}
The problem is that you get a null expection when you try to use a function for a null object.
I had to change the script a little bit. It looks like this now:
function OnTriggerEnter (other : Collider) {
Detonator ;detonator = gameObject.GetComponent("Detonator");
if(detonator!=null){
detonator.Explode();
yield WaitForSeconds(2);
Destroy(this.gameObject);
}
}
It demanded that I add a semicolon after the first Detonator and that I insert another bracket at the bottom (For the OnTriggerEnter part). Now it's saying "(2,1): $$anonymous$$ identifier: 'Detonator" despite me now having a detonator script on the object.
You probably had a variable called detonator already, do it like this:
function OnTriggerEnter (other : Collider) { Detonator localDetonator = gameObject.GetComponent("Detonator"); if(localDetonator !=null){ detonator.Explode(); yield WaitForSeconds(2); Destroy(this.gameObject); } }
I arranged it like this:
function OnTriggerEnter (other : Collider) {
Detonator localDetonator = gameObject.GetComponent("Detonator");
if(localDetonator !=null){ detonator.Explode();
yield WaitForSeconds(2);
Destroy(this.gameObject);
}
}
I got the same error as before, "(2,1): BCE0005: $$anonymous$$ identifier: 'Detonator'."
In addition, having this script saved causes compiler errors across all of my scripts.
This is really strange... With this information I can't help you, I would need to see the whole script to see the problem.