If we pass a prefab (reference) as the GameObject on an Animation Even call. Will we get an instance of that prefab? And will it create a new instance for every event call?
A reference to a prefab is just a reference. You need to call Instantiate() to create an instance of it. If you’re just passing the prefab asset reference as an argument into a function, that won’t generate any new game objects in your scene.
Right, I knew that from working with in the scene. For some reason, I assumed that the gameObject would be null until instanciated. That does not seem to be the case. So how can you tell if you are working with the Asset (instance?) or the game instance?
Never mind I found this on the forums:
- objectType != PrefabAssetType.NotAPrefab &&
- prefabInstanceStatus == PrefabInstanceStatus.NotAPrefab;
That’s probably not going to work. Notice that those enums are in the UnityEditor namespace, which means they won’t work in a build. They’ll seem to work fine when you’re running within Unity, but as soon as you try to create a build, it will fail, because you’re not allowed to use UnityEditor code in a build.
Does it really matter if you’re dealing with a prefab reference or something else? In either case, you’re probably just going to call Instantiate() to make a copy of it. Otherwise, why are you passing around these objects? Do you sometimes create new instances, and sometimes just do things to the object itself?
I am experimenting with different ideas. So this is all about making sure I am understanding this (prefabs) properly.
Good point on the UnityEdtor part I had missed that.
There is an answer to finding out if an object is an instance or a prefab or the prefab “template”, it seems a little “hacky”, but here is it in case it helps someone:
-
someObj.gameObject.scene.name == null
-
scene.rootCount == 0