Search Unity

Find prefabs root gameobject

Discussion in 'Scripting' started by indie_mike, Aug 22, 2018.

  1. indie_mike

    indie_mike

    Joined:
    Nov 7, 2016
    Posts:
    29
    Hey everyone,

    I have been looking for a way to find the root gameobject of a prefab at run-time but I am have trouble finding a good solution.

    For example a scene hierarchy will be:

    Object1
    Object2
    Prefab Instance
    Child1
    Child2
    Child3

    I have no way of telling which layer of the prefab I will be selecting OnClick as it is an environment prefab. So i could be colliding with Child1, Child 3 or any other level depending how deep it goes.

    The problem is I need to know what the 'root' game object of the prefab (In this case it'll be Prefab Instance) is because it has a script attached to it which will give me information on the current selected prefab.

    Is there any way of finding this out through unity's API or do I have to do a custom script? So far transform.root doesn't work for this case and PrefabUtility is an editor script so guessing that wont work either.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Snowirbis, ixmas93 and indie_mike like this.
  3. indie_mike

    indie_mike

    Joined:
    Nov 7, 2016
    Posts:
    29