Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

When to use Capital and Lowercase G.

Discussion in 'Scripting' started by puddleglum, Oct 25, 2020.

  1. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    380
    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
     
    welpWot likes this.
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Kurt-Dekker likes this.
  3. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    380
    ok thank you that clears it up :)
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    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. :)
     
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,904
    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:
    Code (CSharp):
    1. GameObject g1;
    2. GameObject player, enemy;
    3. 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.
     
    PraetorBlue likes this.
  6. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    380
    lol thank you
     
  7. puddleglum

    puddleglum

    Joined:
    May 11, 2020
    Posts:
    380
    ok makes kindof sense :)