- Home /
How to know if a GameObject is a prefab?
I have a a list of objects via **Resources.FindObjectsOfTypeAll**.
I want to know who are Prefabs and who are on scene.
@Veehmot Is Ghost prefab? Resources.FindObjectsOfTypeAll ().FirstOrDefault (g =>g.transform.hideFlags==HideFlags.HideInHierarchy);
@$$anonymous$$ax-Pixel gives the right answer. if (someObj.gameObject.scene.name == null) Debug.Log("It's a prefab!");
Answer by Dreamora · Feb 18, 2012 at 06:52 PM
In the editor, use PrefabUtility.GetPrefabParent(theGO); ( http://unity3d.com/support/documentation/ScriptReference/PrefabUtility.html ) if you have a game object
At runtime you can't find it out any longer and its not needed cause you can not create or modify them anyway
Alternatively, don't use this function to get the resources but specific type ones. Anything thats a texture, mesh, audio or movie file is never a prefab, anything else so all component / monobehaviours are always prefabs as they can't exist in resources otherwise
Just for completeness
PrefabUtility.GetPrefabParent(gameObject) == null && PrefabUtility.GetPrefabObject(go) != null; // Is a prefab
Thanks.
"At runtime you can't find it out any longer and its not needed cause you can not create or modify them anyway"
I don't think this is true, at least any more. I've run into the exact same problem as the OP. I need to find all cameras in the scene active or inactive and make changes to them. Using FindObjectsOfTypeAll it also finds the prefabs. When I make modifications during runtime like this then stop the player, those changes persist in the Prefab :O
@Veehmot That wrong, because you don't say what is go, but it resembles test for prefab original. It will return false for prefab instance. See my answer for corrent solution for all prefab states.
It's true for the original question! Wanted to differ between scene objects (that includes prefab instances) and prefabs. But thanks for your answer it explained to me what the two functions mean :)
I was wondering about the "At runtime you can't find it out any longer and its not needed cause you can not create or modify them anyway" policy.... Why restricting the users from a check? I mean even though you cannot create or modify them, you might want to know in a method that the inco$$anonymous$$g GameObject is a prefab or a scene instance. I know that probably with a quick scene check you could tell, or writing your own GamoObject extension to do so, but what I do not understand is the decision of secluding the option out of the box.
You want bugs on runtime? because that's how you get bugs on runtime
Answer by Max-Pixel · May 23, 2016 at 05:33 AM
if (someObj.gameObject.scene.name == null) Debug.Log("It's a prefab!");
I'm pretty sure checking scene.rootCount == 0
is also a viable option.
I haven't tried but i guess a GameObject created by script also get the same result
In my tests, GameObjects that are instantiated from script do get a reference to the scene in which they were instantiated, so their scene.name
is not null.
This was really helpful for my custom editor code to check if inspecting a project asset or a scene object. Thanks!
How about check if it is the root object of the prefab instance at the same time, in game?
Answer by Roland1234 · Sep 22, 2013 at 11:51 PM
I don't know if this was more recently added functionality, but it'd be much better to use: PrefabUtility.GetPrefabType(Object) The posted code:
PrefabUtility.GetPrefabParent(gameObject) == null && PrefabUtility.GetPrefabObject(go) != null;
will return true for a prefab instance in the scene that has been disconnected, which may not be what you'd intend.
You're right, that's the simplest solution:
if (PrefabUtility.GetPrefabType(go)==PrefabType.Prefab)
You even can tell whether it's a prefab, instantiated prefab and much more...All other options are workarounds.
PrefabUtility exists in the UnityEditor namespace, so don't expect this solution to work in game code that is built. It will only work in scripts that are meant to be run as "tools" in the editor, and not ultimately included in the shipped game.
Answer by aeroson · Jan 02, 2016 at 02:11 AM
Proper working and tested solution for I hope all states of prefab:
using UnityEditor;
bool isPrefabInstance = PrefabUtility.GetPrefabParent(gameObject) != null && PrefabUtility.GetPrefabObject(gameObject.transform) != null;
bool isPrefabOriginal = PrefabUtility.GetPrefabParent(gameObject) == null && PrefabUtility.GetPrefabObject(gameObject.transform) != null;
bool isDisconnectedPrefabInstance = PrefabUtility.GetPrefabParent(gameObject) != null && PrefabUtility.GetPrefabObject(gameObject.transform) == null;
This is a good solution, but the transform is not required. I used this:
bool IsPrefabInstance(GameObject go) {
return PrefabUtility.GetPrefabParent(go) != null && PrefabUtility.GetPrefabObject(go) != null;
}
Answer by Xtro · Oct 09, 2014 at 04:37 AM
Edit: Today I found out about GameObject.hideFlags so it can be used for finding if the gameobject is prefab or not. if hideFlags is HideInHierarchy then it's a prefab. Please ignore the rest of this post...
I found the answer at last!!! (Play mode only)
internal static bool IsPrefab(this Transform This)
{
var TempObject = new GameObject();
try
{
TempObject.transform.parent = This.parent;
var OriginalIndex = This.GetSiblingIndex();
This.SetSiblingIndex(int.MaxValue);
if (This.GetSiblingIndex() == 0) return true;
This.SetSiblingIndex(OriginalIndex);
return false;
}
finally
{
Object.DestroyImmediate(TempObject);
}
}
I posted the whole code in here : http://forum.unity3d.com/threads/i-found-the-solution-for-checking-if-a-gameobject-is-prefab-or-not.272958/
Thanks, it's a good idea to check GameObject.hideFlags! And it works in runtime, like Max-Pixel's solution.
Your answer
Follow this Question
Related Questions
Get the name of an instance's prefab at runtime? 0 Answers
Mark gameobject field as changed from prefab 2 Answers
How does Unity retain UnityEngine.Object references? 2 Answers
Why my prefab is auto changing? 3 Answers
Very Slow (hang) gameobject TO prefab operation with lot of child objects 0 Answers