I’ve been trying to call PrefabUtility.GetPrefabAssetType from inside a MonoBehaviour’s Start function but it always returns NotAPrefab even though the MonoBehaviour is attached to a prefab. Is there even a way to detect if a GameObject is a prefab in play mode with a MonoBehaviour? Funny thing is that It does work in an Editor script in play mode.
The attachment is a prefab made with Unity 2019.2.19f1.
Run the sample scene and check out the console. The information printed says that the JustSomePrefab is not a prefab and is not part of any prefab. Lies :).
I also created a PrefabInfo script that will show prefab info. It displays the correct info when attached to the prefab in the scene.
Is this a bug, or as designed? If it’s as designed, can I request a feature to detect if a game object is a prefab in play mode from a MonoBehaviour?
Thanks for the response grizzly! None of those solutions are working for me. I even tried the “gameObject.scene.name” one.
To be clear, I only need this to work while running the game from inside the Editor. I don’t need to use it in a build.
PrefabUtility works when you use it inside a class that inherits from Editor, but it does not work inside a class that inherits from MonoBehaviour.
The reason this situation is kind of odd is that the legacy PrefabUtility.GetPrefabType does return the correct information when you use it in a class that inherits from MonoBehaviour. It’s the new prefab methods that don’t work like I’d expect. This leads me to believe that this was just an oversight.
Edited: I take that back. It doesn’t work with PrefabUtility.GetPrefabType either.
So close, and I was so hopeful! It kind of does the same thing that PrefabUtility does. When you’re not in play mode it sets the IsPrefab correctly. But as soon as it runs, the class is reallocated and IsPrefab automatically retains the default value because it’ll never reach the IsPartOfAnyPrefab line.
lol, doh! Your example uses a public variable which automatically serializes. I made mine a private property so nothing can set it except the class it lives in. I just needed to add the [SerializeField] attribute and now it works
Thanks for your help! I wasn’t even aware of the OnValidate method, and that was the ticket!