- Home /
What is the type of my prefab?
I'm confused about the type of objects in my scripts. I have a prefab called Enemy, with a script called EnemyScript. Unity seems pretty happy when I pass the prefab in with type EnemyScript. But isn't that just one component of Enemy? What would happen if there were multiple scripts associated with Enemy -- could I bring in the prefab with any of these types? Could I set the type of the prefab to simply be Enemy?
When my script assigns a value to transform, shouldn't it really be setting transform of the gameObject with which the script is associated? Or it that what's happening, and Unity is just being clever and doing that mapping for me? It feels weird that sometimes I refer to an object by one of its scripts. Likewise, sometimes it seems like I'm meant to refer to an object by its transform.
So what is the type of my prefab? Is it a gameObject, any script in the prefab, the name of the prefab itself, a transform, or something else? And if more than one of these is feasible, what are the implications of the various options?
Any help would be much appreciated. Thanks very much in advance.
Good job crafting such a clear and thorough expression of the question.
Answer by Bunny83 · Apr 02, 2012 at 06:27 PM
Basically it's always a GameObject, but you can use any reference to a component on that Gameobject as well. Components can't live without it's host GameObject. If you clone (instantiate) a component, it will clone the whole host GameObject with all it's Components and child objects.
When declaring a variable of type EnemyScript you can drag the gameobject on this variable but the editor will search for a component of that type and assign the reference to the component instead of the reference to the GameObject.
Answer by Lednar · May 28, 2018 at 08:41 PM
To give some extra details, I saw in Unity manual in Instantiating Prefabs at runtime a code that says:
// Require the rocket to be a rigidbody.
// This way we the user can not assign a prefab without rigidbody
public Rigidbody rocket;