- Home /
OnCollision Works for one script but not the other?
Hi, I've been trying really hard to get this part of the script to work but I just don't understand why it wont work here are my scripts:
The one that works:
void OnCollisionEnter (Collision other)
{
print("hit");
if (other.gameObject.tag == "Bullet")
health -= 1;
}
The one that doesn't:
void OnCollisionEnter (Collision other)
{
print("hit");
if (other.gameObject.tag == "Player")
Destroy(gameObject);
}
What I did was put both of these scripts into one object and let the player just run into it. It prints out "hit" only for the first script but not the second. I took out the first script and when testing it again no hit is printed.
I have boxcolliders in both of my objects and 2 rigidbodies. One stands still the other (the player) runs into it.
I do not understand why the second script prints nothing. Any ideas?
Are both scripts active i.e. the little tickbox in the inspector is ticked?
Yes both were active. But I did notice something weird when i deactivated the 1st script it was still ran.
If in doubt add a print statement to both script's Start()
method.
Okay so I did that to the fist script and it doesn't print the start function but does print out hit still.
If Start()
is not running but other methods are, it sounds like "Start" might be misspelt or have the wrong parameter signature.
Answer by Tekksin · Dec 25, 2013 at 07:58 AM
void OnCollisionEnter (Collision other)
{
if (other.gameObject.tag == "Player")
Destroy(gameObject);
print("hit");
}
I don't really script like you do. I just do "function OnCollisionEnter (other: Collision){}" It may not be saying anything because you don't have it in the event brackets for if(other.gameObject.tag == "Player"){}.
Aside from that, just make sure the tags are right.
Answer by mkjrfan · Dec 22, 2013 at 04:25 PM
Is there really no solution to this? Has anyone else had this problem before?
Perhaps the second script is not on the object with the collider?
No, sorry they are both on the same objects with a collider and rigidbody.