Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Access a specific gameobject or clone

Discussion in 'Scripting' started by LuisKaiser88, Sep 23, 2022.

  1. LuisKaiser88

    LuisKaiser88

    Joined:
    Sep 23, 2022
    Posts:
    4
    Hello All,

    in my unity script I want to generate different gameobject and clones of gameobject but I want to identify every object with a specific Id maybe as tag or name.

    So when I received an event from a tcp client holding an id, i want to destroy this one specific gameObject or clone with the id that was passed. Thats why I have to identify this objects globally.

    Hope you can help me and show a small example, cause I think this code won't be so much.
    I am new in Unity.

    Thak you very much for your help!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Just make a script to store references to what you created in a Dictionary, perhaps this:

    Code (csharp):
    1. Dictionary<int,GameObject> CreatedObjects = new Dictionary<int,GameObject>();
    Make sure that callback is on the main thread or it won't work with any of the Unity API (such as Destroy).

    Delegate queue to marshal tasks from other threads to the main thread:

    https://forum.unity.com/threads/how...-everytime-it-is-called.1148354/#post-7370375