- Home /
OnCollisionEnter not working
I'm making a teleporter. The script is below. For some reason nothing's happening. The debug.log doesn't trigger either. I'm kinda stumped.
here's the script:
function OnCollisionEnter (other : Collider)
{
if (other.tag == "Player")
{
teleportTo.position = other.position;
Debug.Log("it works");
}
}
p.s the teleporter has a rigidbody and a collider atached.
Answer by robertbu · Jun 19, 2014 at 06:47 PM
OnCollisionEnter() has a Collision for a parameter, not a collider. So the code should be:
function OnCollisionEnter (other : Collision)
{
Debug.Log(other.gameObject.name + "," + other.gameObject.tag);
if (other.gameObject.tag == "Player")
{
teleportTo.position = other.transform.position;
Debug.Log("it works: "+teleportTo.position);
}
}
I added/embellished your Debug() statements so that you get more data about the collision.
Hey, sorry for the late response. When I implemented this, I got a bunch of errors regarding the Debug() statements, so I commented them out. Then nothing happened. I Tested it and well, nothing.
The whole point of the Debugs is to try to show you what is happening - by removing them you're in the dark again... What exactly do you do with teleportTo after it's been set?
There was a missing '+' sign on line 3. I fixed it. Assu$$anonymous$$g you've declared 'teleportTo', the script fragment should now work.
Your answer
Follow this Question
Related Questions
How to implement teleportation w/o collisions 1 Answer
Collision Detection not working properly 2 Answers
Whats wrong with my score script 1 Answer
Collide detection with tag [JS] 0 Answers
Car dynamics? 0 Answers