Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 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
6
Question by just4unity · Jun 08, 2012 at 03:00 AM · gameobjectprefabtype

Determine if game object is tyoepf One Prefab

Hi, I am building a precondition system,and need to listening to the stage when game object add to stage,and to check if the game object is type of my pre need prefab.

presudo code

 if(typeof(newAddedGO) == typeof(myNeedPrefab))
 {
   NeedSatisfied();
 }

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

3 Replies

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

Answer by Bunny83 · Jun 08, 2012 at 03:20 AM

I have no idea what you actually want to test. A prefab is not a type. A prefab is just an object instance like every other object in the scene. Also at runtime there is nothing like a prefab "connection". You can't determine from which prefab a certain object got cloned. At runtime there are only off-scene objects (your prefabs) that can be used as cloning source and scene objects which are your actual objects that are rendered / updated ...

So what you want to do isn't possible at all. You would have to invent your own property to identify a certain prefab. For example create a script with a prefabname variable and attach it to the prefab. Now you can name your prefab and check the name which is of course the same on the cloned object. A more direct way is to use tags and compare them

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 just4unity · Jun 08, 2012 at 03:47 AM 0
Share

Thanks Bunny

avatar image Bovine · Feb 20, 2013 at 09:34 PM 0
Share

Okay but let's say I've tagged a bunch of prefabs with a script, how can I tell which are prefabs, i.e. NOT instantiated and which are in the hierarchy?

avatar image Bunny83 · Feb 20, 2013 at 11:32 PM 0
Share

@Bovine: Well, as i said at runtime there's no difference between instance and prefab because both are actual objects but prefabs are not placed in the scene and don't take part on any events (rendering, collisions, update, ...).

In the Editor you can deter$$anonymous$$e if a certain object is a prefab or not. Just use PrefabUtility.GetPrefabType on your object reference and check the result

Since this is an editor utility it can only be used in editor scripts.

avatar image Bovine · Feb 20, 2013 at 11:34 PM 0
Share

Hmmm, no use to me sadly - looks like prefabs have a negative instance ID, but I've seen that change between platforms...

avatar image Bunny83 · Feb 21, 2013 at 12:50 AM 1
Share

Well, as far as i know Awake isn't called on non-scene objects. So since you have script attached to the prefab you could simply add a variable "isPrefab" and set it to "true" by default. In Awake you set it to false. So each instance will have the value false and only prefabs will still return true.

Show more comments
avatar image
7

Answer by midtown · Mar 08, 2014 at 07:26 AM

I accomplished this in a silly way, but it works for me. Using your variables:

 if (newAddedGO.name == string.Format("{0}(Clone)", myNeedPrefab.name) {};

This just uses the fact that an instantiated "Foo" Prefab will get the name "Foo(Clone)", unless you change it.

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 Beugnen · Jun 20, 2021 at 03:54 AM 0
Share

If you are seeing "(Clone)" in the Hierarchy it means the object is a clone and not an instantiated prefab and is most likely caused by calling Instantiate instead of PrefabUtility.InstantiatePrefab in scripts marked with [ExecuteInEditMode].

See Instantiating prefabs creates a clone, not instance of the prefab for more information.

avatar image
2

Answer by Rollie42 · May 26, 2020 at 11:41 AM

The accepted answer is unnecessarily dismissive, and no longer correct. See https://forum.unity.com/threads/test-if-prefab-is-an-instance.562900/

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

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

9 People are following this question.

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

Related Questions

What is the type of my prefab? 2 Answers

Hide/Unhide Game Object 4 Answers

How would I change the value of one prefab's variable via script without changing all of them? 1 Answer

Source Gameobject rendered when using prefab 0 Answers

Objects flickering between dark and light 7 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