In my game I have a bunch of ball like objects floating around the scene. I want it so that when these objects collide they go from two objects of specified masses to one object of the sum of their masses. The problem I am having is that when they collide the triggerEnter method is called by both of them. Here is my code in C#
void OnTriggerEnter2D (Collider2D other)
{
if (other.name.Contains ("Food"))
{
var foodTransform = Instantiate(food) as Transform;
foodTransform.position = transform.position;
FoodScript f = foodTransform.GetComponent<FoodScript>();
f.Direction = Direction;
f.mass = mass + (int)(other.gameObject.transform.localScale.x / 0.02f);
f.speed = speed;
foodTransform.localScale = new Vector3 (f.mass * 0.02f, f.mass * 0.02f, 1f);
Destroy(gameObject);
Destroy(other.gameObject);
}
This at the moment results in two new objects both of the correct size, but one continues bouncing around where as the other is created in the position of the collision but does not move as the original object that would have created it is destroyed before the script of this object is started. I do not know how to handle this so that this goes from two objects to one. Can someone help me with this problem?