- Home /
Check if other object has fired a Collision Event
Hey, I'm wondering if there's a way to check if a referenced object has collided with something. I know you can do this with a fairly simple script on the referenced object, but I want to know if it's possible to do this without setting up said script on the referenced object itself.
The reason I need this is because I'm making a simple pickup script using Joints to move an object around. Now I need this object to detatch when I smash it hard enough against a surface. I can't use the breakForce inherited on the Joint as it'll break if I move it too fast. So my idea is to check in my Pickup Script if the object I'm holding is colliding.
Pseudo Example:
public GameObject heldObject;
if(heldObject.collider.CollisionEnter)
{
//DoStuff
}
Answer by meat5000 · May 13, 2014 at 11:12 AM
Yes. Search google for 'Unity Collider'.
In the Collision functions, the Collider Argument is a container for the item that was hit by the collider. You have various access to this object through a call to the Collision. There are widespread examples of this on t'internet and right here on UA.
https://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
I already know of this and that's what I said in the beginning of my post, this would be my "2nd solution".
That's the exact opposite of what I want. I want to run a script on my Camera, and then check if the object I referenced is colliding with something (That's not the camera). So something like
public GameObject heldObj;
if(heldObj.collider.CollisionEnter)
{
//DoStuff
}
I've had a look and in my opinion you can not directly access the Collision state of a collider from a script in another object due to the way CollisionInfo is passed to the OnCollision Event functions. There does not seem to be a .colliding variable.
You can read or implant the variable value using GetComponent.
This can be achieved by making a Collision variable and copying the info to it during the Collision Event. Then this information will be available on any Object you wish to view provided this routine is in their script.
//Script on Colliding Object
#pragma strict
var otherCol:Collision;
function OnCollisionStay(col:Collision)
{
otherCol = col;
}
//Script on Observing Object
#pragma strict
var ob1 : GameObject;
var ob2 : GameObject;
var otherObjectScript : OCSTest;
var testCol : Collision;
function Start ()
{
ob1 = GameObject.Find("skeleton$$anonymous$$ecanimed");
ob2 = GameObject.Find("skeleton$$anonymous$$ecanimed1");
otherObjectScript = ob1.GetComponent(OCSTest);
}
function Update ()
{
testCol = otherObjectScript.otherCol;
Debug.Log(testCol.gameObject + " COLLISION!");
}
You could use a raycast or screentoworld point or something to acquire the objects ins$$anonymous$$d of a GameObject.Find
There probably are more elegant and cheaper solutions, but I wanted to give an example of what you could do. Hit me up if you know another way!
Answer by theredace · May 13, 2014 at 08:37 PM
Couldn't you just toggle a bool variable on/off as the object enters/exits the collider and check that variable state?
Yes, much the same way as above, but with limited information.
Your answer
![](https://koobas.hobune.stream/wayback/20220613144930im_/https://answers.unity.com/themes/thub/images/avi.jpg)