- Home /
How to detect collision between two objects from a separate script?
In my game I want a gate to close (which is an animation) when the player steps on a platform. I don't want scripts on all three of those objects just to do one simple task, so how can I see if the player and the platform are colliding from a script on the gate?
Comment
Answer by AmritH · Jun 04, 2015 at 02:12 PM
You could add a colour to one object in the script and a different colour to the other object it is supposed to be colliding with. for example,
void OnCollisionEnter(Collsion2D col){
if(col.gameObject.name == "nameOfObject2")
Debug.Log("<color=red>Object1 Hits!!</color>");
}
and in the script for object 2,
void OnCollisionEnter(Collsion2D col){
if(col.gameObject.name == "nameOfObject1")
Debug.Log("<color=blue>Object2 Hits!!</color>");
}
Hopefully that helps you out. :)