- Home /
OnCollisionEnter function problem. JS
Hi, Im have a game object with lots of children (as a 2d person with 'limbs' containing rigid bodies) and an object which hits it, but i need to detect when the collision occurs.
I am using the following script:
#pragma strict
function Start (){
}
function Update (){ var other : Collision; OnCollisionEnter(other);
}
function OnCollisionEnter(other : Collision){ if (other.gameObject.name == "character"){ Debug.Log("Smash");
}
}
The problem is that firstly, I get an error saying the if(other.gameObject.name == "character") line 'object reference not set to an instance of an object', and also the function doesn't work as the Debug.Log isn't activated when the objects collide. I have tried using tags and the name of the whole character with the children inside, but the problem persists, any ideas? cheers guys :)
Answer by rutter · Mar 31, 2012 at 02:10 AM
This part seems like a very bad idea:
function Update ()
{
var other : Collision;
OnCollisionEnter(other);
}
Firstly, you should probably avoid calling collision functions from your own code -- how will you distinguish between those calls and actual collisions?
Secondly, you're passing in a null reference, which seems to be causing that error you're getting.
Answer by ratboy · Mar 31, 2012 at 12:03 PM
Of course! Seems so obvious now, I think I saw that on someone else's script and copied it across... Thanks a lot I'll sort it in the morning and let you know if it works, thanks for the quick response.