My script keeps giving me an error when I destroy an object tagged “iceblock” in my game. Says Missing Reference Exception: object of type “box collider” has been destroyed but you are trying to access it. I believe I’m checking whether or not the collider is null. Maybe it’s the lack of sleep that’s getting to me but does anyone see my error?
function OnCollisionEnter(collision : Collision) {
var renderers = GetComponentsInChildren(Renderer);
if (collision.gameObject.tag == "iceblock") {
if(collision.gameObject != null) {
Physics.IgnoreLayerCollision(0, 11, true);
Destroy(collision.gameObject);
audio.PlayOneShot(explodeSound);
for (var r : Renderer in renderers) {
r.enabled = false;
}
Instantiate (explosion, gameObject.transform.position, Quaternion.identity);
yield WaitForSeconds(2);
Physics.IgnoreLayerCollision(0, 11, false);
Destroy(gameObject);
}
}
}
Dude, I think You can not use gameObject.transform.position in the Instantiate!
Since you are destroying it and then calling it again.
Ya it was just me being tired. Sorry for the post. It was another part of my script conflicting with this. What I posted above will work on its own if anyone else is looking for something similar.
But here is how I solved the problem:
I had a line in my script defining a general collision:
if (collision) {
//perform collision code
}
This was the issue. The fact that I was declaring two collisions that technically worked at the same time was causing a problem because it would detect a general collision and perform the code and then perform the collision with the game object tagged “iceblock.” So I added an else statement to the script and problem solved :
if (collision.gameObject.tag == "iceblock") {
// perform collision code
}
else if (collision) {
//perform collision code
}