Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
11
Question by Veehmot · Feb 18, 2012 at 01:58 AM · gameobjecteditorprefabresourcesfindobjectsoftypeall

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image winxalex · Jan 13, 2016 at 03:35 PM 0
Share

@Veehmot Is Ghost prefab? Resources.FindObjectsOfTypeAll ().FirstOrDefault (g =>g.transform.hideFlags==HideFlags.HideInHierarchy);

avatar image tangwentian · Jul 07, 2017 at 10:12 AM 0
Share

@$$anonymous$$ax-Pixel gives the right answer. if (someObj.gameObject.scene.name == null) Debug.Log("It's a prefab!");

15 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Veehmot · Feb 23, 2012 at 12:50 AM 3
Share

Just for completeness

 PrefabUtility.GetPrefabParent(gameObject) == null && PrefabUtility.GetPrefabObject(go) != null; // Is a prefab

Thanks.

avatar image xpation · Mar 14, 2013 at 01:06 PM 3
Share

"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

avatar image aeroson · Jan 02, 2016 at 02:09 AM 0
Share

@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.

avatar image v01pe_ aeroson · Aug 26, 2016 at 10:21 AM 0
Share

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 :)

avatar image kornel-l-varga · Jan 28, 2018 at 01:18 PM 0
Share

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.

avatar image RowelCaps · Dec 21, 2020 at 09:08 PM 0
Share

You want bugs on runtime? because that's how you get bugs on runtime

avatar image
31

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SunnyChow · May 23, 2016 at 06:43 AM 0
Share

I haven't tried but i guess a GameObject created by script also get the same result

avatar image Max-Pixel SunnyChow · May 23, 2016 at 06:59 AM 1
Share

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.

avatar image tangwentian · Jul 07, 2017 at 10:10 AM 0
Share

Very cool, good answer. Thanks.

avatar image howong · Apr 02, 2018 at 07:26 AM 1
Share

This was really helpful for my custom editor code to check if inspecting a project asset or a scene object. Thanks!

avatar image martinrhan1 · Dec 15, 2019 at 10:44 AM 0
Share

thank you so much for this

avatar image realaldrick · Oct 07, 2021 at 06:28 PM 0
Share

How about check if it is the root object of the prefab instance at the same time, in game?

avatar image
11

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jake-L · Aug 15, 2017 at 04:53 PM 0
Share

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.

avatar image Max-Pixel · Aug 15, 2017 at 05:08 PM 1
Share

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.

avatar image
8

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;
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Gru · Jan 12, 2017 at 10:32 AM 0
Share

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;
 }
avatar image
3

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);
         }
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xtro · Oct 09, 2014 at 05:04 AM 0
Share

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/

avatar image volodya_p · Apr 05, 2021 at 12:16 PM 0
Share

Thanks, it's a good idea to check GameObject.hideFlags! And it works in runtime, like Max-Pixel's solution.

  • 1
  • 2
  • 3
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Load/Unload assets in Editor? 0 Answers

Prefabs are instantiated in editor but not in executable 2 Answers

Automatically update prefab instance 1 Answer

Get the name of an instance's prefab at runtime? 0 Answers

Mark gameobject field as changed from prefab 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges