- Home /
How to create one object, when two collide?
I have a Player, who creates simple projectile 2D gameobjects as his children. I need to create one big projectile, when two small collide, with direction equal to the average. How do I code this? I already have an array of objects in player class (max number of projectiles is 3, so it's ok using the array)
I tried using a function in Player object, to instantiate big projectile and call it in OnCollisionEnter2D of the small projectile, but it calls once for each projectile and creates two Big ones, idk how to fix it.
Answer by azza696 · Jun 08, 2020 at 08:07 AM
A solution could be to differentiate each projectile by an attribute such as an int id when you create them. Then when two projectiles collide, only the one with the lowest id create a big projectile.
Unity's object already has such attribute
https://docs.unity3d.com/ScriptReference/Object.GetInstanceID.html
Your answer