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 /
avatar image
0
Question by Pulsar19 · Dec 05, 2018 at 09:58 AM · gameobjectloopnamecreatedelete

GameObject.find("name") doesn't work if executed in the same loop of...

Hello,

I have a big problem : When I run the following code manually, it works.

   if (Input.GetKeyDown(KeyCode.Space))
             {
                 // do something and delete some gameobjects
             }
   if (Input.GetKeyDown(KeyCode.M))
             {       
                 for (int i = 1; i <= popini + 1; i++) 
                 {
                     if (!GameObject.Find("SpherePreset_" + i))
                     {
                         // do something and create some gameobjects this name
                     }

But if I run it in a loop (in one time), it doesn't work ...

   if (Input.GetKeyDown(KeyCode.Space))
             {
                 // do something and delete some gameobjects
                 
                 for (int i = 1; i <= popini + 1; i++) 
                 {
                     if (!GameObject.Find("SpherePreset_" + i))
                     {
                         // do something and create some gameobjects with this name
                     }

In this code, unity doesn't enter in the loop where I create my name for my new gameobjets... I think, for Unity, the Gameobjects still exist because the delete of gameobjects is done in the same loop of the create gameobjects.

Is there a solution please ?

Comment
Add comment · Show 1
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 Bonfire-Boy · Dec 05, 2018 at 10:36 AM 0
Share

I would recommend removing your dependence on the Find function altogether. Ins$$anonymous$$d, keep track of objects yourself in a manager class of some sort.

BTW your theory about what's going on here could be tested by putting the code into a coroutine and yielding after the deletion.

1 Reply

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

Answer by kaarloew · Dec 05, 2018 at 11:00 AM

Delete happens in same frame, so you have to delay the create/instantiate. Or better yet, use object pooling.

There is also DestroyImmediate https://docs.unity3d.com/ScriptReference/Object.DestroyImmediate.html

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 Pulsar19 · Dec 05, 2018 at 11:17 AM 0
Share

Thank you !

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

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

How to obtain a gameObject from an List with the name? 1 Answer

Problem to rename gameobject when loading from file. 0 Answers

Can I play multiple AudioSources from one gameobject? 8 Answers

Get the name of an instance's prefab at runtime? 0 Answers

multiple targeting 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