- Home /
onCollisionEnter not working
I am trying to use onCollisionEnter to figure out if an object is colliding with another with a tag of pickup. Here is my the code I have that is attached to the object I want to be detecting collision.
function Update () {
}
function OnCollisionEnter(con : Collision) {
if(con.collider.tag == "pickup"){
Pickup = true;
}
}
I'm not getting an error. But the OnCollisionEnter never seems to be going. I have a rigid body attached the the gameobject. I have tried putting a print statment into the function as the first line but I never get anything back. Any ideas what I'm doing wrong?
Answer by murkantilism · Mar 15, 2013 at 07:58 PM
First double check that the object's tag is spelled "pickup", I think this is case-sensitive.
If a typo isn't the error, double check that your script is actually attached to a GameObject inside the scene.
If both of these checks are verified, then I'm not sure. You'd need to post more of your code or where/how it's being used.
Answer by Josie · Mar 15, 2013 at 09:10 PM
On line 7 change
function OnCollisionEnter(con : Collision) {
to
function OnCollisionEnter(other : Collision) {
then change line 8 to
if(other.tag == "pickup"){
Im not sure if you stated your var but if you didn't then do this
var Pickup : boolean = false
Hope this helps
Why would changing the name "con" to "other" make a difference?
And declaring the boolean var ahead of time is good practice, but shouldn't be a problem.
Answer by Polinator · Mar 15, 2013 at 09:27 PM
I think
if(con.collider.tag == "pickup"){
should be changed to
if(con.gameObject.tag == "pickup"){
At least that's how my colliders work.
You may want to look at the Script Reference, in case you're missing an attached component to one of the objects. Also make sure you're not mixing up OnCollisionEnter() with OnTriggerEnter(), I have done that a few times myself.
Also, if you still can't get anything to work, use
function OnCollisionEnter(con : Collision) {
print (con.gameObject.tag);
//if(con.collider.tag == "pickup"){
//Pickup = true;
}
To see what you're colliding with. And uncomment if it's working as intended.
Answer by whydoidoit · Mar 15, 2013 at 09:56 PM
The rigidbody attached to this script must not be asleep (not moved for a while) or must be hit by something that is moving and has a rigidbody and a collider.
Your answer
Follow this Question
Related Questions
Does OnCollisionEnter only get called when the scripted object initiates the collision? 4 Answers
Response speed in OnCollisionEnter / OnTriggerEnter 1 Answer
Testing for collisions prior to placing an object 0 Answers
Collision detection general approaches in Unity3D for C# 1 Answer
Delayed Collisions Bug (includes video demonstration) 1 Answer