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

How to find referances to an gameObject which is in the Hierarchy

Discussion in 'General Discussion' started by gokhan, Nov 6, 2022.

  1. gokhan

    gokhan

    Joined:
    Jun 21, 2013
    Posts:
    5
    Hi,
    for example I have a gameobject in the hierarchy. And in the game it is being hidden. How do you find which objects in the hierarchy have a referance to this gameobject?
    Please keep in mind that, the gameobject is not a prefab, so I can not use the function "Find References In Scene". And lets assume that this gameobject is not being referenced during runtime.
    Thank you
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,617
    Assuming that the references are serialized, you could find the ID of the object in question, then search for that ID occurring in the serialized data for each GO in the hierarchy.

    I am unaware of a generic tool for that being built in.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    C# (and most of OOP) isn't really built well to answer the question "what can see me". So you are going to have to do something ugly and hacky.

    The most obvious solution is to save the project and delete the GameObject. Run the scripts. See which components throw a null reference error... Of course this won't pick up references that aren't actively used during the time frame of the test.

    If your reference is set up in the inspector, you can search the scene file for the GameObjects ID. Honestly doing ctrl+F on a simple text editor might be the fastest way to do this.

    For a comprehensive test you will need to use reflection to scan all of the objects in the hierarchy with all of their references. This would be a hell of a job to set up.
     
  4. gokhan

    gokhan

    Joined:
    Jun 21, 2013
    Posts:
    5
    Thank you
     
  5. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Add a Debug.Log() to OnDisable() on a script of that object. You'll be able to see which code is disabling the object in the printed call stack on the console.
     
    lmbarns and angrypenguin like this.