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:

Code:


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" :

Code:


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();


Really need help on this, any ideas or suggestions are welcome. Thanks in advance Wink