Failure to destroy a game object in a for each loop

i am writing a program where i collect collision info on a stay collision. i then use that info to create an array of masses that have hit the main object. the problem I’m having is that the for each loop where i check the masses to be equal to the main object’s mass is not destroying the game object that is equal to the main object.

using UnityEngine;
using System.Collections;


public class Cubeinteractions : MonoBehaviour {
	
void update(Collision collisioninfo){
	
		ArrayList allmasses = new ArrayList();
		foreach(Collision rigidbody in collisioninfo){
			float boxmass = collisioninfo.rigidbody.mass;
			allmasses.Add(boxmass);
		}
		
		foreach(float boxmass in allmasses){
			if(boxmass == rigidbody.mass){
				Destroy(collisioninfo.gameObject);
			}
			
		}
	}
	
}

I am new to unity and C# to boot so i am unaccustomed to debugging things that are not syntax or scope errors and would much appreciate any help in discovering the issue

Collision is the type for your collisioninfo. It is not an array of Collisions (unless I’m mistaken). So it would contain only one Collision. If it’s not the same mass you are looking for, it won’t destroy. I don’t know how you are 'collecting collision info’s but I would think it would be in an Array or List, and that’s what you should be looking through.

That said, what are you trying to do? If you want something to destroy when it hits this thing, if it has the same mass, just implement OnCollisionEnter and check the masses then, if equal, destroy.

Looking again at your post, it seems you want to destroy the object when the TOTAL of the masses hit is equal to ‘this’ object’s mass? If so, you may want >= instead of = because it could go over and not detect equality. And to get the total, you need += on that total.

boxmass += blah blah blah