- Home /
The question is answered, right answer was accepted
Get collision without attaching a script to every game object
I have a bunch of objects in my scene. The player can choose which object to move at any given time. I need to detect if the moving object is colliding with any of the other objects - this would be fairly simple with OnCollisionEnter() script attached to the moving object, but, since the player can move any of the objects, I would have to attach the same script to all of them. I'm looking for a work around. To put it more simply, say there are objects A and B, which collide, and there's an object C with a script on it, which returns true if said two objects collided. A and B have no scripts attached.
You can access the collider and hence the whole gameobject of a colliding object through an object colliding with it.
A player can know what he collided with.
You can not, however, access this information through a body not involved in the collision. You will need to add a routine to the colliding objects which passes on the information if you wanted to access this from the player.
I suppose it'd be too good to be able to get collision information from an object not involved. Although, the collision still has the data, even if no scripts are attached, it is still visible and apparent. I just don't have any idea to properly access it.
While this doesn't really solve anything, I'm glad to at least know that this idea will not work, therefore I will not spend another hour or two aimlessly searching. Please post this as an answer, so I could accept it. Thank you for you help :)
Wait, what about hierarchy? The parent object is not directly involved in collision, but maybe I could find out if the child is colliding through the parent? Catching straws here.
Answer by meat5000 · Jan 25, 2015 at 09:09 PM
The data is only really accessible through the OnCollision routine. This is why a script is needed; as when the function has finished executing the information is no longer accessible as its scope was limited to the function itself.
You will need to create a variable of type Collision outside of the OnCollision function and use this field to hand off he information.
Alternatively, pass it straight to a player or another object, nonetheless you will need to script it to access the data.
Follow this Question
Related Questions
The force on a collider which is penetrating the other collider 1 Answer
Destroy the gameObject after seconds when not hit. 0 Answers
Unity Colliders are Overlapping/Intersecting 2 Answers
Collision Only being detected on one of the objects involved in the collision - C# 0 Answers
Colliding two GameObjects 1 Answer