Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Jake_Martin_ · May 20, 2016 at 08:42 PM · gameobjectprogrammingloop

Problems with loops

Ok, this is likely to be a pretty difficult one for you to help me with, but, anyway... I'm making a game. At one point, you fly into a portal, stuff happens. One of them things is it should destroy all objects with tag 'obstacle'. This works perfectly the first time. The second time, however, it doesn't. Here's the code:

         rocks = GameObject.FindGameObjectsWithTag("obstacle");
 
         for (var i = 0; i < rocks.Length; i++)
         {
             Destroy(rocks[i]);
         }

I know the function that that's contained in is run both times, but... like I said, the obstacles stay the second time round.

Weirdly, two other loops are run when the player flies into the portal in another script and neither of them work the second time either. Here's one:

 foreach (Transform child in transform)
         {
             GameObject.Destroy(child.gameObject);
         }

and here's the other:

  foreach (GameObject i in Planets)
         {
             i.transform.parent = transform;
         }

So, I know the problem seems pretty vague, but I'd appreciate any ideas. I'm completely clueless as to why they'd only work once.

Thanks.

EDIT: Just to include what I said below, changing that second code snippet to

foreach (GameObject i in Planets) { Destroy(i); }

fixes it.

Still stuck with the other two though...

Comment
Add comment
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

2 Replies

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

Answer by NornIndustries · May 20, 2016 at 09:06 PM

Depending on how quick you fire this event the second time, it may be that Unity is being slow on garbage collection. I noticed recently that calling Destroy() doesn't guarantee that the object will be destroyed right then.

So what you can do is to first make it that when a rock is created it is also added to a static list of rocks. Then when you do your foreach, you both destroy the objects on that list AND remove them from the list (important!). Then when it checks the list the second time they at least won't be on the list, even if they are still sort of existing somewhere waiting to be destroyed. I hope that helps!

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 Jake_Martin_ · May 20, 2016 at 09:23 PM 0
Share

The event is fired anywhere from about 10 seconds to a few $$anonymous$$utes later, so not so quickly. Putting all rocks in a list may be a bit difficult. There are groups of rocks as children of separate objects, so while adding them to a list in the script attached to their parent is easy, collating that list and then removing the groups from the list on time in the correct order could be quite problematic.

EDIT: Interestingly, changing the second snippet of code to foreach (GameObject i in Planets) { Destroy(i); } made that work.

avatar image
0

Answer by Jake_Martin_ · May 20, 2016 at 09:48 PM

I fixed this. The problem was to do with not removing planets from their list properly after destruction. Changing that made everything else work :)

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to reference assets that are inside a folder?,Referencing assets in the scene 0 Answers

Camera Issue,Camera is changing it's position in game view. 0 Answers

Input Command Issues 0 Answers

My gameobject stops moving after entering the trigger?? 2 Answers

How to project a semi transparent copy of a 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