- Home /
How to check if an object has collided with a specified object from a script
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.
This approach seem a bit odd. What are you trying to accomplish? Perhaps there's a better way?
Answer by Meltdown · Mar 26, 2011 at 06:21 AM
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");
}
}
Thanks for the response. Would it be possible to get that code in JS?
I have added the Javascript way of doing it, using the Send$$anonymous$$essage command. Please mark my solution as the answer if it helped. Thanks
How can I get the following to work properly:
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Floor") // So the bullet is on the floor and not sitting on some crate or whatever
{
if (timer > 300f) // So the bullet has almost certainly come to a complete standstill or isn't moving enough to notice
{
Destroy(bulletCollider);
Destroy(bulletRigidbody);
Debug.Log("Bullet is on floor with collision and rigidbody deactivated");
}
}
}
If I set the timer to a low value then everything works and I see the debug text because the collision has happened after the timer has reached the specified value. But if I set the timer to a higher value, where the bullet might have bounced a few times and come to rest then it fails and the debug text doesn't show?
Basically, I want the bullet shell to turn off some of its components after a certain time--so the game isn't processing things like colliders and rigidbodies unnecessarily--but only so long as it's already on the floor. And after that is done I don't want to run the code again. This is all just so I can have a bunch of bullet shells laying around without taking up any real resources. But, I do actually want them to remain there rather than just disappear or fade out after a few seconds as happens in most fps games.
Your answer
![](https://koobas.hobune.stream/wayback/20220613114923im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
super mario pipe physics? 1 Answer
Trouble with OnCollisionEnter and Exit 1 Answer
destroy touch the screen on gameobject in camera 0 Answers
Audio play when object hit collision. 2 Answers