- Home /
OnCollisionEnter doesn't work
Hi. I have a sphere gameobject with a sphere collider, mesh renderer, rigidbody. Then i have a cube gameobject with a box collider, mesh renderer, rigidbody, my script attached to it as well.
I have some gui text, everytime the sphere collides with the cube i want a variable called points to be incremented. Here is my script, i dont understand why it isn't working, everytime the sphere hits the cube, the gui text remains: points 0. thanks for any help.
var points: int;
var refText: GameObject;
function Start ()
{
points =0;
refText.guiText.text="Points: "+points;
}
function OnCollisionEnter(collision : Collision)
{
if(collision.gameObject.tag=="myCubes")
{
Debug.Log("Ball hit Box");
points++;
}
}
function Update ()
{
refText.guiText.text="Points: "+points;
}
Answer by wolis · Mar 07, 2013 at 08:08 PM
Locate the problem first. Is it a problem with GUI drawing or point counting? Does the collision return "Ball hit Box" to your Debug log?
Thanks for your reply , it doesn't return "Ball hit Box", does that mean there is a problem at the point counting ?
ok i got it to work by changing:
if(collision.gameObect.tag=="myCubes")
to :
if(collision.gameObject.name=="Ball")
and it works successfully.
However when i attach the same script to another cube the gui text just doesn't increment, however it does print "Ball hit Box" to the log. I don't understand why it wont work, any help is appreciated
Your answer

Follow this Question
Related Questions
OnCollisionEnter without disabling isKinetic 1 Answer
Why is my OnCollisonEnter not working? 1 Answer
How can I keep track of score without each new basketball shot reseting the points? 0 Answers
How can I keep track of score without each new basketball shot reseting my points? 1 Answer
How to make a counter in GUI ? 5 Answers