Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by Squishlove · Oct 15, 2012 at 10:02 PM · c#gameobjectdestroyforeach

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Oct 16, 2012 at 12:34 AM 0
Share

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?

avatar image Kryptos · Oct 16, 2012 at 09:22 AM 1
Share

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:

  1. Foreach loop are used to read data from a collection.

  2. 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.

avatar image Squishlove · Oct 27, 2012 at 06:48 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Squishlove · Oct 27, 2012 at 06:42 PM 0
Share

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).

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

How to only delete one of two collided objects? 1 Answer

Destroying an object after its out of sight ! 2 Answers

C# List foreach problem 1 Answer

deleting self-gameobject,deleting self-gameobject 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges