• 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
2

Answer by _geo__ · Sep 18, 2019 at 11:27 AM

Just for completeness, since Unity 2018.3.0 the use of "PrefabUtility.GetPrefabType(...)" is discouraged.

You should now use one of the new PrefabUtility.Is... methods which are nicely explained by Unity Dev Steen Lund at Unity LA 2018 ( https://youtu.be/HxbSJ-EIjXI?t=1226 ).

So your new check may look like this:

 /// <summary>
 /// Checks whether or not the object has anything to do with prefabs.
 /// </summary>
 /// <param name="go"></param>
 /// <returns></returns>
 static bool IsPrefab( GameObject go )
 {
 #if UNITY_2018_3_OR_NEWER
     return PrefabUtility.IsPartOfAnyPrefab(go);
 #else
     return PrefabUtility.GetPrefabType(go) != PrefabType.None;
 #endif
 }
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
1

Answer by slake_it · Jan 31, 2019 at 04:34 AM

@Max-Pixel Thanks for the solution, a slightly better method would be


  if(gameObject.scene.IsValid() == false)
             {
                 Debug.LogError("this is probably a prefab, might change values permanently, will return");
                 return;
             }
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
1

Answer by Romeno · Apr 24, 2019 at 03:05 PM

There is new API PrefabUtility.GetPrefabInstanceStatus(object) which returns either NotAPrefab, Connected, Disconnected or MissingAsset.

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 Max-Pixel · Apr 24, 2019 at 05:30 PM 1
Share

Note that PrefabUtility is from the UnityEditor namespace, so this answer does not work at runtime.

avatar image
1

Answer by v01pe_ · Dec 26, 2020 at 08:56 PM

You are right @shieldgenerator7 here's my verions in the form of an extension method:

 public static bool IsPrefab(this GameObject go)
 {
     return go != null && (PrefabUtility.IsPartOfPrefabAsset(go) || PrefabStageUtility.GetPrefabStage(go) != null);
 }

Also works, if the prefab is opened in the prefab editor.

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
0

Answer by lgarczyn · Oct 03, 2019 at 05:57 PM

I have not seen this solution posted, but there are a lot of new options in PrefabUtility, namely:

 IsPartOfPrefabAsset                // if the given object is part of a Prefab Asset.
 IsPartOfPrefabInstance             // if the given object is part of a Prefab instance.
 IsPartOfRegularPrefab              // if the given object is part of a regular Prefab instance or Prefab Asset.
 IsPartOfVariantPrefab              // if the given object is part of a Prefab Variant Asset or Prefab Variant instance.
Comment
Add comment · Show 3 · 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 BradZoob · Nov 22, 2019 at 03:08 PM 0
Share

Yeah that's absurd right there, monolithic descriptive future deprecation horror. Be nice if they'd make these binary so a simple check for zero means it's nothing to do with prefabs, rather than what looks to be a future tome of flags. Weee! try to guess which gets deprecated first!

avatar image BradZoob BradZoob · Nov 22, 2019 at 03:10 PM 1
Share

Ah sorry, seems there's more and at least there's: "IsPartOfAnyPrefab" :P

avatar image lgarczyn BradZoob · Nov 23, 2019 at 05:01 AM 0
Share

Oops, missed it. But yeah, while some parts of the engine can be clean, the whole package/asset/scene/prefab/subprefab/Instance system is kind of a mess

  • ‹
  • 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

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

Why my prefab is auto changing? 3 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