Why don't you have to instantiate a class when using gameobject class

I am new to Unity and this is very confusing. In the scripting reference, gameobject is defined as a class; however, why do we not create a new instance when to use gameobject or other class? Is it all static variables or something?
for example i think we just do:
without creating a new instance, directly accessing the class?

What I think is that it should be:
Transform myTransform=new Transform();

But we never do it like this nor it works.

and one other question.
Where in unity tells functions like Update() and Start() do their stuff.
Like how does unity know that it should run the Update function every second?


GameObject is a class and you create a new instance of it just like any other class, using a constructor:

var gameObject:GameObject = new GameObject("name");
GameObject gameObject = new GameObject("name");

Nothing special.

Components on the other hand are very special. Because of their close tie to the runtime (with Update etc), can only be created using the AddComponent function of a GameObject. The Transform component is even more special, as you can only ever have one and a GameObject cannot exist without it. All this stuff is part of the quite overly complicated runtime that makes scripting that much easier.

When you do something like:

transform.position += Vector3.one;

What you’re actually doing is this:

this.gameObject.GetComponent<Transform>().position += new Vector3(1, 1, 1);

Where gameObject is the link between the component and the GameObject it is attached to and
GetComponent() finds a component of type Transform that is attached to the GameObject.

All components, even MonoBehaviour, inherit from Component. MonoBehaviour is then how Unity can call Update() and Start(). A new “script” component inherits from MonoBehaviour overriding the methods Update, Start (etc) allowing for the system to work.

Basically the big features that make Unity work the way it does are: Inheritance and specialized Constructors.

Hope this helps,

PS: Update runs every frame, not every second :wink: