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();
}
The accepted answer is unnecessarily dismissive, and no longer correct. See https://forum.unity.com/threads/test-if-prefab-is-an-instance.562900/
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.
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