Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Rickenbaker · Feb 21, 2017 at 11:39 PM · c#instantiatedestroytagsprefab-instance

[Solved] Destroy instantiated prefabs with a same tag, one by one, from last instantiated to first ?

Hello all,

I am trying to destroy instantiated prefabs with a same tag, one by one, from last instantiated to first, and I am stuck on how to accomplish this.

The idea is that I want to have a custom editor, which would allow me to instantiate multiple prefabs and then "undo" the last instantiation with two GUI buttons - "Place object" and "Undo", respectively.

As of now, I am able to successfully instantiate prefabs, add the same tag to them, and then destroy them one by one, but they are getting destroyed from first to last instantiated.

The code I have so far:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ObjectInstantiateControl : MonoBehaviour {
 
     public List<GameObject> prefabList = new List<GameObject>();
     [HideInInspector]
     public int objectSelectionIndex = 0;
     GameObject instance;
 
 public void PlaceObject()
     {
         switch (objectSelectionIndex)
         {
             case 1:
 
                 Debug.Log("Just received a number 1 from the editor");
                 GameObject object_A = Resources.Load("Object_A") as GameObject;
                 instance = Instantiate(object_A, this.transform.position, this.transform.rotation,  this.transform);
                              
                 instance.tag = "UsedObject";
                 prefabList.Add(instance);
 
                 break;
 
             case 2:
 
                 Debug.Log("Just received a number 2 from the editor");
                 GameObject object_B = Resources.Load("Object_B") as GameObject;
                 instance = Instantiate(object_B, this.transform.position, this.transform.rotation,  this.transform);
                           
                 instance.tag = "UsedObject";
                 prefabList.Add(instance);
                 break;
 
             case 3:
 
                 Debug.Log("Just received a number 3 from the editor");
                 GameObject object_C = Resources.Load("Object_C") as GameObject;
                 instance = Instantiate(object_C, this.transform.position, this.transform.rotation,  this.transform);
                              
                 instance.tag = "UsedObject";
                 prefabList.Add(instance);
                 break;
 
             case 4:
 
                 Debug.Log("Just received a number 4 from the editor, deleting the object");
 
                 prefabList.Remove(GameObject.FindWithTag("UsedObject"));
                 DestroyImmediate(GameObject.FindWithTag("UsedObject"));
                 break;
         }
     }

}

And the part from the editor script for GUI buttons "Place object" and "Undo" :

                          GUILayout.BeginHorizontal();
 
                         if (GUILayout.Button("Place object", GUILayout.Height(25)))
                         {
                             ObjectInstantiateControl myScript = (ObjectInstantiateControl)target;
                             myScript.objectSelectionIndex = 1;
                             myScript.PlaceObject()();
 
                         }
 
                         if (GUILayout.Button("Undo", GUILayout.Height(25)))
                         {
                             ObjectInstantiateControl myScript = (ObjectInstantiateControl)target;
                             myScript.objectSelectionIndex = 4;
                             myScript.PlaceObject();
 
                         }
                         GUILayout.EndHorizontal();

Any ideas or suggestions are welcome. Thanks in advance ;)

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 LiloE · Feb 22, 2017 at 12:09 PM

Use a Stack to manage undo

A Stack is a last-in-first-out structure. Use it like this:

 Stack<GameObject> undoStack = new Stack<GameObject>();
 
 // whenever you instantiate a new object, push it to the stack
 undoStack.Push(theObject);
 
 // on undo, just pop the last one and destroy it.
 var objToUndo = undoStack.Pop();
 Destroy(objToUndo ); // tip: avoid using DestroyImmediate. It's better to disable the object before destroying it if you must have it disappear immediately.
 


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 Rickenbaker · Feb 22, 2017 at 12:24 PM 0
Share

Thanks LiloE, that is exactly what I was looking for!

However, when trying to first disable and then Destroy(objToUndo), the object gets disabled but not destroyed, and I get the message "Destroy may not be called from edit mode! Use DestroyImmediate ins$$anonymous$$d.", while using DestroyImmediate works like a charm.

One more thing, is there a way to avoid the InvalidOperationException: Operation is not valid due to the current state of the object System.Collections.Generic.Stack message once there are no more gameObjects in the stack and the "Undo" button is clicked?

Thanks again!

EDIT: No worries, I was trying so hard to overthink it, when I realized that a simple IF statement will do the trick!

 if (undoStack.Count > 0)
             {
                 GameObject objToUndo = undoStack.Pop();
                 DestroyImmediate(objToUndo);
             }


 


avatar image
0

Answer by Zee-Play · Feb 22, 2017 at 04:52 AM

I would create a Gameobject list and destroy the last gameobject in the list every time you press undo.

Look for game object list here on answers, should find the solution you need.

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

321 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 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 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 set instantiated game object as child on collision in C# 0 Answers

How do I destroy a GameObject/Script that instantiates other objects, without destroying the objects it has previously instantiated? 0 Answers

instantiate a cloned prefab in another scene 0 Answers

Destroy prefabs after they have been instantiated (C#) 1 Answer

Unable to destroy instantiated prefab during OnMouseExit,Unable to Destroy previously instantiated object OnMouseExit() 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