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

Get Prefab from GameObject of that Type

Discussion in 'Prefabs' started by NissStudios, Jul 22, 2019.

  1. NissStudios

    NissStudios

    Joined:
    May 15, 2017
    Posts:
    4
    If I have a reference to a GameObject instantiated from a prefab, how can I use this to source a reference to the prefab?
    so
    GameObject prefab(clone)

    to
    GameObject prefab
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,364
    I was going to post something, but as it turns out, this page says both things I wanted to say.

    https://answers.unity.com/questions/1107653/how-to-find-the-original-prefab-of-a-gameobject.html

    And in case the Answers site is phased out (which is the rumor), in short, you can only determine it with editor-specific code, not runtime code. At runtime, an object is an object, and a prefab object is just an object which isn't in a scene. The instantiate call does not make note of the original when creating a new instance clone. A workaround would be to add some sort of "hi, i know i am a prefab, here is my source" add-on and try to apply it to all your prefabs ahead of time.
     
  3. NeatWolf

    NeatWolf

    Joined:
    Sep 27, 2013
    Posts:
    924
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,364
    If you scroll all the way up the left column you will notice that PrefabUtility is really
    UnityEditor.PrefabUtility
    , so code using it needs to be in an Assets/.../Editor folder or inside an
    #if UNITY_EDITOR
    block. Both would be stripped from a build.

    Again, at runtime, there is no such distinction as a prefab.
     
    runevision likes this.
  5. NeatWolf

    NeatWolf

    Joined:
    Sep 27, 2013
    Posts:
    924
    I did scroll, and answerer right because in that moment I was removing a few warnings in a few assets (which only worked at edit time and conditional compilation was obviously used), but I was kind of in a hurry, and the original poster didn't provide enough info to justify spending more time to check :p

    Yup, exactly. Working at runtime wasn't in the request, but at runtime there's no such thing like prefabs... in ways useful to solve the problem with a single line of code.

    @NissStudios , do these answer your question?
     
    Last edited: Jul 22, 2019
    aliasmorton likes this.
  6. NissStudios

    NissStudios

    Joined:
    May 15, 2017
    Posts:
    4
    @NeatWolf Yes this does, I did mean a solution for run-time so I guess I will have to include a reference to the prefab for the instantiated clone to pass. Shouldn't be too difficult just a bit more work as things start to scale. Thanks for the help.
     
  7. sajjadgameactor

    sajjadgameactor

    Joined:
    May 27, 2015
    Posts:
    8
    If you want to find prefab variant of an instance you should use
    Code (CSharp):
    1.   PrefabUtility.GetCorrespondingObjectFromSource(YourPrefabInstance)
    If you want to find orginal prefab of an instance you should use
    Code (CSharp):
    1. PrefabUtility.GetCorrespondingObjectFromOriginalSource(YourPrefabInstance)
    It will work only in Editor mode.