Hello, everybody. I am trying to make a script that executes a command when object A collides with object B. The script, however, needs to be on object C. Any pointers on how to do this would be great.
1.) Create a script called ObjectCScript, and attach it to object C.
2.) Create a function on ObjectCScript, called ABCollided()
3.) Create a script called ObjectAScript, and attach it to object A.
4.) Add the following lines of code to ObjectAScript...
public ObjectCScript objectCScript; // Set this in the inspector
void OnCollisionEnter(Collision theCollision)
{
if(theCollision.gameObject.name == "ObjectB") // If A collides with B
{
objectCScript = objectCScript.GetComponent<ObjectCScript>();
objectCScript.ABCollided(); // Call the ABCollided function on ObjectB
}
}
You can also use SendMessage to call a script method on another object.
Here is a JavaScript example
Add this script to ObjectC..
function ABCollided()
{
Debug.Log("A collided with B");
}
Then add this script to ObjectA...
function OnCollisionEnter(collision : Collision) {
if(collision.gameObject.name == "ObjectB") // If A collides with B
{
gameObject.Find("ObjectC").SendMessage("ABCollided");
}
}