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 tony1308 · Jan 25, 2018 at 09:14 PM · destroygameobjectclonesinstantiating

Removing clones instantiated from prefab

Hi, I am trying to make a function that will destroy all prefab clones when i press one button. Here is code but when I try to run it, Unity crashes...I tried different things but it just don't work. Hope that someone will help.

  private void ClearScreen()
     {
         if (GameManager.instance.shouldCloneBeDestroyed == true)
         {
             while (GameObject.Find("food(Clone)") != null)
             {
                 Destroy(foodClone);
             }
         }
     }
     private IEnumerator Spawner()
     {
         gameObject.SetActive(true);
         yield return new WaitForSeconds(startWait);
         while (!stop)
         {
             randFood = Random.Range(0, 7);
             Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
             foodClone = Instantiate(food[randFood], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
             foodClone.transform.parent = transform; 
             Rigidbody2D rigidBody = foodClone.GetComponent<Rigidbody2D>();
             spawned++;
             yield return new WaitForSeconds(timer);
         }
     }

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 calpolican · Jan 25, 2018 at 09:51 PM

It would be more performance-effective creating a tag "FoodClone". Say, when you instance your prefab, you can use:

 foodClone.tag = "FoodClone"

Searching by tags takes less resources than searching by name, and you can easily put them into an array. Then to delete them you use:

 GameObject[] foodClones = GameObject.FindGameObjectsWithTag("FoodClone"); 
     foreach(GameObject foodClone in foodClones)
        GameObject.Destroy(foodClone);

if the foreach trhows any problem you can use a while loop instead:

 while (foodClones.Length >0)
 {GameObject.Destroy(foodClones[0]);}

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 tony1308 · Jan 25, 2018 at 10:24 PM 0
Share

Ty very much, this is working like a charm :)

avatar image
0

Answer by LTonon · Jan 25, 2018 at 09:24 PM

Do you ever set stop to true on the Spawner Coroutine? If not, the problem is there and the endless loop crashes unity.

Also, doing Destroy(foodClone) seems wrong to me since you are not actually assigning what you found using GameObject.Find("food(Clone)") to anything after you find it... Making the foodClone variable not what you are expecting. Try (not tested):

 private void ClearScreen()
 {
     if (GameManager.instance.shouldCloneBeDestroyed)
     {
         foodClone = GameObject.Find("food(Clone)");
         
         while(foodClone != null) 
         {
             Destroy(foodClone);
             foodClone = GameObject.Find("food(Clone)");
         }
     }
 }
Comment
Add comment · Show 3 · 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 tony1308 · Jan 25, 2018 at 09:44 PM 0
Share

Still crashes Unity...If you know solution please help me because I am desperate

Here is code i didn't write above:

     public static SpawningFood instance;
 
     public GameObject[] food;
     public Vector3 spawnValues;
 
     private float startTime;
 
     public bool stop;
 
     private int randFood;
     private int spawned = 0;
 
 
     [HideInInspector]
     public GameObject foodClone;
 
     private void Awake()
     {
         if (instance == null)
         {
             instance = this;
         }
     }
     private void Start()
     {
         StartCoroutine(Spawner());
     }
     private void Update()
     {
         TimerCtrl();
         ClearScreen();
     }

avatar image LTonon tony1308 · Jan 25, 2018 at 09:52 PM 0
Share

Yeap, the problem is that you never stop your loop on the Coroutine. Try using Invoke ins$$anonymous$$d, seems better in you case, so...

 private void Start ()
 {
     Invoke("Spawner", waitTime);
 }
         
 void Spawner() 
 {
     gameObject.SetActive(true);
     randFood = Random.Range(0, 7);
     Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 1, Random.Range(-spawnValues.z, spawnValues.z));
     foodClone = Instantiate(food[randFood], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
     foodClone.transform.parent = transform; 
     Rigidbody2D rigidBody = foodClone.GetComponent<Rigidbody2D>();
     spawned++;
     Invoke("Spawner", waitTime);
 }
avatar image tony1308 LTonon · Jan 25, 2018 at 10:25 PM 0
Share

Ty for effort but it crashed Unity again, guy bellow gave me solution :)

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

75 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

Related Questions

Destroying clone object in list does not remove the first one 0 Answers

instantiate a texture2d so changes don't affect clones 1 Answer

How do I delete a certain number a clones from my scene? 1 Answer

Instantiating a projectile pointing at target. 1 Answer

Instaniate Prefab 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