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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Class Instances clarification

Discussion in 'Scripting' started by KoalaBear0825, Feb 8, 2019.

  1. KoalaBear0825

    KoalaBear0825

    Joined:
    Feb 8, 2019
    Posts:
    1
    So i'm reading a marvelous book authored by Jeremy Gibson and I have come across his expample of Class Instances. Now here is the example;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    //Defining the class Human
    public class Human
    {
    public string name;
    public Human partner;
    }
    //public variable declaration
    public class Family : MonoBehaviour
    {
    public Human husband;
    public Human wife;

    void Start()
    {
    husband = new Human();
    husband.name = "Keala Arscott";
    wife = new Human();
    wife.name = "Hannah-Paige James";

    husband.partner = wife;
    wife.partner = husband;

    husband.name = "Keala Kekai Arscott";
    wife.name = "Hannah-Paige Arscott";

    print (wife.partner.name);


    }
    }

    So right off the bat, I have a few questions.
    1. what entails public string name; and the line below it?
    2. in husband = new Human(); is he only using new to tell C# that it is literally a new instance of the variable?

    Thank you so much for taking your time and helping me. i'm a fresh newborn when it comes to code and unity in general. it means bunches!
     
  2. carl010010

    carl010010

    Joined:
    Jul 14, 2010
    Posts:
    139
    KoalaBear0825 and angrypenguin like this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,519
    Both of these lines are declaring variables. A variable is a named location in memory where you will store a piece of data.

    Variable declarations at the class level have the following form:
    Code (csharp):
    1. [access modifier] [type] [name];
    The access modifier can be "public", "protected" or "private". I'm sure your book will get to those at some point. In Unity, if a compatible variable is public then it will appear in the Inspector, where you can tweak its value for an instance without modifying the code.

    The type can be any valid type declared for C#. That's a huge topic on its own, hopefully your book will clear that up when you get to the relevant part. A "string" is the programming term for a sequence of text characters, so usually a string variable holds some text. Other types are "int" (whole numbers), "float" (numbers with a 'floating' decimal point), and things like Unity's Vector3 (which can represent a position or direction).

    The name part is exactly that - you need to give your variable a name so you can refer to it later on in your code.

    The end of any statement is a ";". In most cases this is a little redundant, as we usually have one statement per line, but that's not always the case.

    The "new" keyword tells the compiler to create a new object of whatever type follows it. The "=" sign then assigns the value on its right (the newly created object) to the variable on the left. It's actually a little more complicated than that because "Human" is a "reference type" but, again, your book should cover that when the time comes.

    The important thing to understand at the beginning is that once the Human object is created and put into a variable, you can then use that variable to access whatever is contained in that object.
     
    KoalaBear0825 likes this.