- Home /
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
I would avoid using a function called 'update' as it could be easily confused with the reserved 'Update' function. I would also avoid using a variable called 'rigidbody' that is not an instance of Rigidbody. Also your 'allmasses' array is redundant, why make it at all?
This might not be related to your issue, but never do something that modify a value (or in this case destroy an object) inside a foreach loop. Foreach loop are read-only: no change can be made to the collection that is iterated through.
In your case, you don't have this kind of situation. But as a general use, I use this good habit rule:
Foreach loop are used to read data from a collection.
For loop are used to modify data inside a collection. Or to modify the collection itfself (but with additional caution regarding the value of the loop index).
It makes my code clearer and prevent unexpected bugs/behaviours.
to DaveA: yeah, that is a habit i ought to not have. But i had meant for update to be Update, thanks for pointing that out would have taken me months to notice that. The reason why i have the array is to do checks throughout the array to do other operations.
to $$anonymous$$ryptos: Thanks for that, as a person still learning how to code practically. Things like that are very useful to me and I appreciate the help.
Answer by DaveA · Oct 16, 2012 at 12:41 AM
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
Thank you so much for those tips i will be implementing the very soon. I know allot of what you said is probably very simple to more experienced coders, but to me it was a bit of a revelation. As to why I'm doing this in an array rather just on the oncollison check, I want to latter be able to add in other possible situations. One i have in $$anonymous$$d particularly is to have 4 masses all touching the object at once and then creating 4 new bodies off a prefab with masses equal to the sum of the 4 masses and the main body over 4(within whole numbers for sanity's sake).