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

Resolved A very dumb question.

Discussion in 'Scripting' started by gjaccieczo, Aug 1, 2021.

  1. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    What are the entries after the square brackets when you Debug.Log a list with "subentries" called?
    Code (CSharp):
    1. Debug.Log(exampleList[indexVariable].theEntryAfterTheSquareBracket);
     
    Last edited: Aug 1, 2021
  2. wileyjerkins

    wileyjerkins

    Joined:
    Oct 13, 2017
    Posts:
    77
    Last edited: Aug 1, 2021
    gjaccieczo likes this.
  3. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    sooo with entry after the square bracket you mean, the thing after the dot?
    as in:
    x[y].z
    you want to know what z is?

    after a dot you access any exposed/public variables, methods etc of an object or class
    if you have a list of objects, the square brackets will simply return you one object within that list, at the specified position

    lets say you have defined class "Car" where every car has a public variable "color", and the class "Car" has a Constructor that intakes a Color and writes it to the "color" variable

    and lets say you make a list of cars like
    Code (CSharp):
    1. List<Car> cars = new List<Car>();
    2.  
    3. cars.Add(new Car(Color.red));
    4. cars.Add(new Car(Color.blue));
    5. cars.Add(new Car(Color.green));
    then
    cars[0]
    will output an object of the type "Car"
    on this object you can read the "color" of the car using .color
    so
    cars[0].color will output Color.red

    in other words, the .theEntryAfterTheSquareBracket has nothing to do with the list or the square brackets, you could very well do:
    Code (CSharp):
    1.  
    2. Car firstCar = cars[0];
    3. Debug.Log(firstCar.color);
    4.  
    instead of
    Code (CSharp):
    1.  
    2. Debug.Log(cars[0].color);
    3.  
    both do exactly the same
     
    Last edited: Aug 1, 2021
    Bunny83, gjaccieczo and wileyjerkins like this.
  4. gjaccieczo

    gjaccieczo

    Joined:
    Jun 30, 2021
    Posts:
    306
    Not what i was looking for, but thank you for bringing that up.
    Yep, that's it. I thought there was some specific term for that specifically for lists.