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 /
avatar image
0
Question by SimplyEvert · Mar 18, 2014 at 11:01 AM · gameobjectdestroyfindgameobjectswithtag

FindGameObjectsWithTag finds destroyed gameobjects

Hi, Im creating an algorithm for automathic level generation. In order to evaluate my algorithm I want to run it some amount of times and meassure permormance values. I also need to perform a test each time to see if the level is correct. I do this by comparing how many rooms there are in the level with how many rooms the resposible script reported it would create (this is not very important for my problem, just some background).

The problem is that when I run the algoithm more than one time the GameObject.FindGameObjectsWithTag() returns all the rooms that was ever created. Even the ones that has been destroyed and are no longer in the scene.

Here are the most important parts of my code:

 void Start ()
 {
     while (nrOfRepeats < repeats)
     {
         clearLevel();
         nrOfRepeats++;
         createLevel();
     }
 
     checkConnectivity();
 }
 
 void clearLevel ()
 {
     GameObject[] rooms = GameObject.FindGameObjectsWithTag("Room");
         
     foreach (GameObject g in rooms)
         Destroy(g);
 }
 
 void checkConnectivity ()
 {
     GameObject[] roomsInScene = GameObject.FindGameObjectsWithTag("Room");
 
     Debug.Log(roomsInScene.Count());
 }


Say I would run the while lopp three times and the algorithm would create 10 rooms, 15 rooms and finally 10 rooms. I would only see 10 rooms in the scene, but the output in the console would still be 35. Anyone has any ideas what the problem here is?

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
1
Best Answer

Answer by SimplyEvert · Mar 18, 2014 at 12:55 PM

I found the problem. Apparently when you call Destroy(GameObject), Unity waits one Update frame before it removes the GameObject. Since all my code is executing in Start Update is never called, hence the rooms are never destroyed.

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
avatar image
0

Answer by koray1396 · Mar 18, 2014 at 11:46 AM

It seems you have missing {} in your foreach loop and it's not destroying g.

Comment
Add comment · Show 2 · 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 SimplyEvert · Mar 18, 2014 at 11:55 AM 0
Share

If you only have one line after a statement like foreach, the {} arn't neccesary.

avatar image ajdrew81 · Aug 20, 2015 at 06:02 PM 0
Share

SimplyEvert is correct. In c# {} are not necessary if it is only the one line like above.

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

21 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 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

Trash Collection For Instantiated Game Object 1 Answer

A node in a childnode? 1 Answer

GameObject.FindGameObjectsWithTag still finding destroyed object (C#) 1 Answer

Destroy the first GameObject out of x fired 1 Answer


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