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

Answer by Sky-Foxy · Nov 02, 2016 at 08:54 PM

If you need find object in Prefabs, Material, Scene, by GUID https://gist.github.com/AndreySkyFoxSidorov/977e5b0a91b3bc3e2acf53917e32f410

Click Right-click select "Find GUID in Prefabs Material Scene" the object to find in Prefabs, Material, Scene, by GUID.

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
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?

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