• 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!");

16 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by tinysnake · Mar 27, 2016 at 01:55 PM

Actually, the EditorUtility.IsPersistent(http://docs.unity3d.com/ScriptReference/EditorUtility.IsPersistent.html) is what you want. This function check the Object whether it's stored on the disk or not. prefab is stored on the disk, but any object on the scene is not stored on the disk.

It's way more elegant than any other code above, but if a object in the scene is derived from a prefab, it will return false, but the PrefabUtility.GetPrefabObject will return true.

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 swanninger · Jun 15, 2019 at 06:22 PM 0
Share

EditorUtility.IsPersistent returns false when editing the prefab, so not quite good enough.

avatar image
-2

Answer by winxalex · Jan 13, 2016 at 03:27 PM

U 5.2 EDITOR: UnityEngine.Object obj; PrefabUtility.GetPrefabType (obj); PrefabType.Prefab is UnityEngine.Pefab (hidden type) asset PrefabType.PefabInstance PrefabType.DisconnectedPrefabInstance

RUNTIME Resources.FindObjectsOfTypeAll ().FirstOrDefault (g =>g.transform.hideFlags==HideFlags.HideInHierarchy)

vote https://feedback.unity3d.com/suggestions/scripting-prefab-detection

Comment
Add comment · 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
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
-1

Answer by arealperson63587 · Dec 02, 2014 at 01:03 AM

This is so incomplete, no one much (1 or 2 people total) can use it. How do you scope "PrefabUtility" ?

You can't say using "System.PrefabUtility"

Fragments are worthless without "Environmental Context"

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 Geminior · Dec 05, 2014 at 12:21 PM 2
Share

If you had bothered to follow the listed link, you would have found your 'Environmental Context' as you call it.

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
  • 4
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

33 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 avatar image

Related Questions

Why my prefab is auto changing? 3 Answers

Very Slow (hang) gameobject TO prefab operation with lot of child objects 0 Answers

How to Load/Unload assets in Editor? 0 Answers

Automatically update prefab instance 1 Answer

Get the name of an instance's prefab at runtime? 0 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