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. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here:
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Access a specific gameobject or clone

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

  1. LuisKaiser88


    Sep 23, 2022
    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


    Mar 16, 2013
    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: