Search Unity

How to determine in code that the object is part of prefab in prefab mode

Discussion in 'Prefabs' started by FizzCube, Nov 2, 2018.

  1. FizzCube

    FizzCube

    Joined:
    Feb 8, 2014
    Posts:
    5
    Hi,

    I want to initialize a variable If its part of a prefab and to clear it if its not part of a prefab. (called from OnValidate)

    This should do it but has an issue:

    Code (csharp):
    1. if( PrefabUtility.IsPartOfPrefabAsset(gameObject) ) ...
    The problem I'm seeing is that when in prefab mode, in OnValidate the above code returns False. I presume this is because the root element when in prefab mode is not it self actually a (sub)prefab .. but is there wany way I know know the root element Is in fact it self in a prefab because thats what I am editing!

    Thanks
     
  2. SteenLund

    SteenLund

    Unity Technologies

    Joined:
    Jan 20, 2011
    Posts:
    639