- Home /
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();
}
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
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?
@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.
Hmmm, no use to me sadly - looks like prefabs have a negative instance ID, but I've seen that change between platforms...
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.
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.
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.
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/