im very new to unity and i noticed sometimes having to put GameObject and gameObject. when do i use Gameobject and when do i use gameObject
Maybe someone more qualified can be more exact but anyway:
GameObject = the base class for scene objects in Unity. You use “GameObject” spelling when you add a field or variable for some GameObject in your scripts.
gameObject = a property in component, you use this when you get a GameObject to which your script/component is attached to.
ok thank you that clears it up
What @eses says above is spot-on.
For another example:
The class Transform
is a type of Component that contains position, rotation, scaling and parenting information about a GameObject.
The field transform
is a shortcut property to the instance of Transform on the current GameObject this script is attached to.
There are other examples in Unity, such as Material
being the name of a class, but traditionally the field material
being the shortcut inside a Renderer object that contains a reference to the instance of the Material class.
Another common disparity that can be confusing is the use of this
, which is a shortcut to the instance of the class you are currently in. Some people like to use it a lot, some people only use it when they need to (to clarify ambiguity).
In the case of properties in a MonoBehavior
, you could write:
this.gameObject
to get at the GameObject,
OR
you can just write gameObject
to do the same thing.
Over time you will recognize these potato-potah-to type conventions and learn to filter them out, know when they mean something, know when they can be safely ignored.
Unfortunately some engineers seem unable to filter that out and it impairs their ability to reason about code that does not precisely conform to their expectations about style, but that is a larger discussion for another place.
Backing up a step, GameObject is a type. It starts with a big G because they felt like it. You can use it to declare variables:
GameObject g1;
GameObject player, enemy;
GameObject gameObject;
The last one is like they’re trying to confuse us on purpose. They decided to use the same name as the type, but with the start lower-cased. Yikes! What jerks. But it’s a pattern you can use. For example, when you hit something you get a “Collision” saying what you hit: the Transform, Rigidbody and Collider. What’s the transform named? c1.trans? c1.hitTrans? c1.otherT? No. They named it c1.transform, with a small t. How do you find the Rigidbody you hit? c1.rigBod? c1.rb? They could have used those names, but didn’t. They used c1.rigidbody, with a small r.
Suppose you know r1 has one Renderer. It could be named anything, but almost for sure they picked r1.renderer. That confusing “exact same except for upper/lower” is supposed make things easier for you to guess variable names. They use it whenever a class has 1 obvious thing of that type. If you have a renderer for your head and body, they’d be named headRenderer
and bodyRenderer
. But if you have just “your” renderer, call it renderer.
lol thank you
ok makes kindof sense