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.

what is this referencing?

Discussion in 'Scripting' started by warrenbrandt, Oct 1, 2020.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    381
    as an example is some code:

    DCDestinationMarker DreamDestination;

    I get that DCDestinationMarker is another script, as there is a script called that.
    but what is DreamDestination actually referencing? the entire script? Does it just allow you to use variables etc on the script DCDestinationMarker or is something else going on?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    6,892
    One instance of the class that is defined by that script. A script contains a "class". You can think of this in plain english as defining a "Class of object" aka "type of object". A good analogy is that if the class is a blueprint for a house, an object is an actual house. So that variable you have there is referencing an actual instantiated object based on that class. The script is a blueprint for a DCDestinationMarker, and the variable is referencing an actual DCDestinationMarker.
     
    Last edited: Oct 1, 2020
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    25,914
    To review, this is what that section of the DestinationChecker code does:

    DestinationChecker.png

    If anyone else cares to futz with it, this is the code referenced above. It's just a goofy little package I threw together to demonstrate the idea of a lander game where you pick people up and move them around. Full package attached below.
     

    Attached Files:

    Last edited: Oct 2, 2020
    warrenbrandt likes this.
  4. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    1,591
    I will just offer a slightly different perspective, but nothing that hasn't been said already.
    A class in object-oriented languages, is this abstract notion of how the object is supposed to work. Like a blueprint.
    An object instance, on the other hand, is when you do actually make an object from this blueprint. You can have just one (that's known as a singleton) or many such instances.

    All objects from the same class share the class's code, but have separate data for the fields and properties as specified by the class. What is unique about them is what takes up the memory. Thus you never copy the unchanging stuff.

    However, even a class itself is an object, and can be referred to, however if you manage to do that, that's "behind the scenes" access known as reflection. Normally programs only refer to fields, properties, and methods which can return either references to some instances or simple, primitive data (numbers, text...).

    Some of the fields, properties, and methods can be also static, which means that they do not need an instance to live on, but live in the class definition instead.

    For the compiler to know what a variable will hold, it needs to be declared for a certain type. All classes are types.
    When you say
    PeanutButter instancegjhgf
    PeanutButter is the type and likely some class, while instancegjhgf is the name you've chosen for the actual handler of this reference (aka identifier).

    All variables must have a valid type and a valid identifier. But the type can also be primitive. For example a typical decimal number is usually represented with a keyword
    float
    . This is also known as a primitive type alias. The actual class of that type is in fact called System.Single. Same goes for
    int
    . It is in fact System.Int32.

    Primitive types normally hold primitive values, and are called value types because of this, their values are copied around when passing. But object instances aren't copied like this, because they tend to get big in memory, and are passed 'by reference' instead.

    Therefore,
    DCDestinationMarker DreamDestination;

    is a declaration of a variable, with an identifier named DreamDestination, the whole thing is either a field in another class, or a variable that is local to some method, holding a reference type for any of the instances of a class called DCDestinationMarker. Further, in the top of the file, you can find its 'using' declaration listed, and figure where this class comes from.
     
  5. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    381
    Yes a big thanks to Kurt for coding me an example to study and learn from for my own project....he really knows his stuff!!! Thanks all for the further input.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    25,914
    Of course! Get your game running, we all wanna play a new lander game!
     
  7. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    381
    battling :)
     
    Kurt-Dekker likes this.
unityunity