Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to replace the reference of an original GameObject with its clone in the editor?

Discussion in 'Scripting' started by pleasurehouse, Apr 23, 2021.

  1. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    I have a few scripts that reference a GameObject (player) through serialization.
    Now I see that this is a problem at the moment of making it respawn after its destruction.
    I need a simple way to replace the original GameObject reference with its clone in the editor.
    Does anyone know how to do it?
    Thank you so much!!!


    Here are some examples of my codes:


    Code (CSharp):
    1.  
    2. public class Spawner : EventManager
    3. {
    4.     [SerializeField] private GameObject[] objectsToSpawn;
    5.     private List<GameObject> objectsSpawned;
    6.     //--------------------------------------------------
    7.     void Awake()
    8.     {
    9.         objectsSpawned = new List<GameObject>();
    10.     }
    11.     //--------------------------------------------------
    12.     public override void Start()
    13.     {
    14.         base.Start();      
    15.     }
    16.    //--------------------------------------------------
    17.     public void InstantiateAll()
    18.     {
    19.         foreach (var obj in objectsToSpawn)
    20.         {
    21.             if (obj!=null)
    22.             {
    23.                 var instance = Instantiate(obj);
    24.                 if (instance != null)
    25.                 {
    26.                     instance.SetActive(true);
    27.                     objectsSpawned.Add(instance);                
    28.  
    29.                     SendEvent(new EventInfo(instance.transform, null), EntityEvent.Type.respawn, EntityEvent.Property.end);
    30.                 }
    31.             }
    32.         }
    33.  
    34.     }
    35.     //--------------------------------------------------
    36.     public void DestroyAll()
    37.     {
    38.         foreach (var instance in objectsSpawned)
    39.         {
    40.             Destroy(instance);        
    41.         }
    42.         objectsSpawned.Clear();
    43.     }
    44. }
    45.  
    46.  


    Code (CSharp):
    1.  
    2. //--------------------------------------------------
    3. public class EventInstaller : MonoBehaviour
    4. {
    5.     [Header("Events Senders")]
    6.     public GameObject[] sender = new GameObject[] { };
    7.     List<IEvent> iEvent = new List<IEvent>();
    8.    //--------------------------------------------------
    9.     void Awake()
    10.     {  
    11.         foreach (GameObject obj in sender)
    12.         {  
    13.             IEvent[] events = obj.GetComponents<IEvent>();
    14.  
    15.             foreach (IEvent e in events)
    16.             {
    17.                 iEvent.Add(e);
    18.             }      
    19.         }
    20.     }
    21.     //--------------------------------------------------
    22.     public void Add(IEventListener listener)
    23.     {
    24.         foreach (IEvent e in iEvent)
    25.         {        
    26.             e.OnEvent += listener.Listen;                      
    27.         }
    28.     }
    29.     //--------------------------------------------------
    30.     public void Remove(IEventListener listener)
    31.     {
    32.         foreach (IEvent e in iEvent)
    33.         {
    34.             e.OnEvent -= listener.Listen;
    35.         }
    36.     }
    37. }
    38. //--------------------------------------------------------------
    39.  
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    Can't do it, there's no way to find every reference to a given GameObject (in runtime).

    What you can do is make some sort of proxy or placeholder. Maybe make a "GameObjectReferencer" whose sole purpose is to forward any events or requests it receives to the actual player object. Then you only need to update that one reference.
     
    pleasurehouse likes this.
  3. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96
    Thank you so much for your help!!
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,907
    You shouldn't destroy anything if in a brief moment you will create a similar another one. Not enemy, not player, not items. It's called object pooling.
    Instead of destroying your player, you can just simulate it's destruction, hide it and reset all the parameters you need at respawning from code. It's simpler for you and better for your performance as well.
    If you find it distracting having crucial parameters in code you can always create a "player spawning scriptable object" to config your parameters through the inspector and read your values from there upon spawning.
     
    pleasurehouse likes this.
  5. pleasurehouse

    pleasurehouse

    Joined:
    Oct 15, 2020
    Posts:
    96

    Thanks for your explanation. Yes, that seems like a good idea for the player. And the clones either?

    I'm still not very clear on what the Spawner's true goal is. Because you can initialize the objects by positioning them manually as well ... And as you say you can simulate that they resurrect without having to destroy them ...

    In which cases do you think it is useful to use a Spawner?

    Thank you so much !!