Generally, from what I've learnt, collisions are handled in the script that is attached to a Game Object. In my game, I have 6 game objects that collide with each other making the former idea difficult to implement. I want to make all the game objects prefabs and instantiate them in another script attached to an empty game object. How can I handle collisions between all these 6 game objects in the new script?
Asked
Active
Viewed 628 times
0
-
why would it be "difficult to implement"? what do you want to happen when there is a collision? bouncing-around is automatic – Fattie Apr 17 '16 at 23:21
-
As it stands, only one object has a prefab and I instantiate a number of copies in an empty game object. All game objects collide with each other causing changes in box colliders, sprite renderers, object destruction and creation and applied force are some of things that happen during collision. The tricky thing is for example, if Object A collides with Object B should Object A have the collider or Object B or both? This decision would be simple if only a few objects collided. – Nullititiousness Apr 18 '16 at 04:36
-
I can't follow you at all. If A collides with B, then **both** A and B have colliders. – Fattie Apr 18 '16 at 11:19
-
It might be you are trying to ask this: imagine a typical game with an enemy and a projectile. During play the projectiles hit the enemies. (obviously BOTH have colliders & rigidbodys - everything must have a collider & rigidbody). When they hit, the enemy blows up, for example. Of course, you have the question, do you do that code to blow up the enemy "on the bullet!" or "on the enemy!" There is no big mysterious answer to this, it's just an everyday programming decision - you could do it either way. If that's what you're asking, that's the answer. – Fattie Apr 18 '16 at 11:28
1 Answers
1
I'm wondering the same thing as Joe Blow, but still if what you want to try to do is handle collisions manually (which by the way will make Physics harder since you would have to calculate bounciness, gravity, etc... manually) then you can try this:
public class OtherScript : MonoBehavior
{
public GameObject[] objects; //Your 6 GameObject
void Update()
{
for (int i=0; i<objects.Length; i++)
{
for (int j=0; j<objects.Length; i++)
{
if (objects[i].GetComponent<Renderer>().bounds.Intersects(
objects[j].GetComponent<Renderer>().bounds))
{
//Handle collision
break;
}
}
}
}
}
If this doesn't work then you should also try with GetComponent<MeshFilter>().mesh.bounds
. Note that if your game is 2D you should use GetComponent<SpriteRenderer>().mesh.bounds
instead.

Agustin0987
- 581
- 6
- 15
-
Please check my reply to Joe Blow's comment. I try to avoid checking collisions in the update method, is it possible to create a custom OnCollisionEnter2D method to solve my problem. – Nullititiousness Apr 18 '16 at 10:25
-
Sure is possible to create a custom event for the collision using delegates and event, but you would still have to detect the collision (either by checking bounds or Unity's physics engine) to trigger the custom event. Maybe if you give some more explanation of what exactly you want to achieve, we could suggest some different ideas. – Agustin0987 Apr 18 '16 at 10:31
-
i am baffled by your question @Nullititiousness of course, obviously, there are callbacks when you have a collision http://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html and so on. – Fattie Apr 18 '16 at 11:23
-
is it possible that (for some reason) you are let's say trying to pass all the collision events to perhaps a master script or a script on some main object? if so it is trivial, just use unityEvent, which you use all the time for many reasons in Unity ... http://stackoverflow.com/a/36249404/294884 – Fattie Apr 18 '16 at 11:24
-
hi @Nullititiousness great - be sure to please "Tick" an answer here to close out the QA and keep the board tidy, thanks – Fattie Apr 18 '16 at 19:10
-
@JoeBlow, I was referring to the link from the other stackoverflow question you answered. – Nullititiousness Apr 18 '16 at 19:18
-
hi @Nullititiousness ! if there's only one answer, and it's a good one, it is traditional to TICK it out of politeness. After all, the only reason I discussed the question and that one of the comments was helpful was that Agustin made this good answer. note that you yourself get points for ticking and it means you get less moderation later. cheers! – Fattie Apr 18 '16 at 23:49