- Home /
Does attaching a script to a GameObject makes the GameObject an instance of the class?
Hello Com,
as the title says:"Does attaching a script to a GameObject makes the GameObject an instance of the class?"
I am asking because often we do: public class SpawnManager : MonoBehaviour {
public GameObject gmObj;
//..other code
}
Now if i write a class "Player" and initiate it:
public class SpawnManager : MonoBehaviour {
public Player plObj;
//..other code
}
Does it mean all objects with the Player-class attached to it, become "Player"-Object instances or are these objects, objects with a player class attached to it and not more?
Well, you haven't "initiated" it there. You've declared that Spawn$$anonymous$$anager instances will have a Player object named plObj and that this field is public. You may have noticed that $$anonymous$$onoBehaviours do not allow you to construct one using something like "new Spawn$$anonymous$$anager()".
So, the short answer is "no". To verify this immediately, try casting a GameObject as Player after attaching a Player script / component to it.
Answer by Bunny83 · Apr 03, 2017 at 03:54 PM
Attaching a script or component in general to a gameobject will create an instance of that component. However the gameobject itself doesn't "become" something else. Think of a GameObject like the chassis of a car. It's just the frame without any functionality on it's own. It just has "attachment points" where you can install other components like an engine, driver seat, ect.
Each component is it's own object and it's own instance. To get a list of all components of a specific type attached to a gameobject you can use someGameObject.GetComponents<Type>()
.
The Unity editor offers a quite smart drag&drop functionality. When you drag a gameobject onto a variable of a component type, it automatically checks if the gameobject has a component of that type attached. If it has, it assigns the reference to that component instead of a reference to the gameobject.
Components can't "live" on their own. They always need to be attached to a GameObject. That's why you can't create an instance of a Component manually. Only by using gameObject.AddComponent<Type>()
. When you drag a script onto a gameobject, Unity essentially does exactly that. It internally creates an instance of your component and attaches it to the gameobject.
Answer by meat5000 · Apr 03, 2017 at 03:15 PM
As a Variable, a 'GameObject' in a script is simply an empty memory-box in the shape of a GameObject. It is a variable of that class.
However, if you Instantiate a GameObject it becomes a Game-World Instance object with an attached Transform Component.
Answer by NoseKills · Apr 03, 2017 at 03:35 PM
Nope. A GameObject is always just a GameObject and it only inherits UnityEngine.Object.
A GameObject can have Components attached to it. For example scripts that you write yourself inherit the MonoBehaviour class, which inherits from Behaviour class, which inherits Component class, and thus can be added to a GameObject as a Component and fetched with GetComponent(). Also for example the Transform that is a part of each GameObject inherits Component. You can reference the transform component of a GameObject either by using the property transform
of the GameObject or any component on it or by calling GetComponent<Transform>()
.
Adding a component to a GameObject doesn't change the GameObject itself. Generally in C# the type of an instance or a variable can never change.
A GameObject can have multiple instances of the same Component type (depends on the component) or many different Components attached to it, so it doesn't make sense that the GameObject would change to one of them randomly.
The inspector fields in Unity are smartly designed so that if you drag a GameObject into a field of type X in the inspector, the inspector picks a reference to an object/Component of type X if one is found on the dragged GameObject. Perhaps this is what got you wondering?
Your answer
Follow this Question
Related Questions
How to convert System.Type into an instance or access the class variables 3 Answers
[SOLVED]How to use attributes from array of custom class instances? 1 Answer
Access class from all scripts 1 Answer
Scriptable object gets deleted with a class that holds it? 1 Answer
Set Dirty on class instance? 0 Answers