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

Access List

Discussion in 'Scripting' started by Loff, Jul 12, 2015.

  1. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Hey.
    trying to figure out how to use "List", and I'm starting to get hang of it but I have a couple of questions I'm not able to understand. Have googled, but don't understand the solutions I find.
    Been using Array for a lot, but I want to improve my scripting and use List.

    I have 2x C# scripts called:
    1. People
    2. CreateNewPeople

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class People : IComparable<People>{
    6.  
    7.     public string name;
    8.     public int age;
    9.     public bool female;
    10.     public bool hired;
    11.     public string applying;
    12.     public Sprite picture;
    13.  
    14.     public People (string newName, int newAge, bool newFemale, bool newHired,string newApplying, Sprite newPicture) {
    15.         name = newName;
    16.         age = newAge;
    17.         female = newFemale;
    18.         hired = newHired;
    19.         applying = newApplying;
    20.         picture = newPicture;
    21.     }
    22.  
    23.     public int CompareTo(People other){
    24.         if (other == null) {
    25.             return 1;
    26.         }
    27.         return age - other.age;
    28.     }
    29. }
    30.  

    While in the start void of CreateNewPeople I have this for example.
    Code (CSharp):
    1.         List <People> people = new List<People> ();
    2.  
    3.         people.Add ( new People ("Harvey", 32,false,false,"Development",boyCharacters[1]));
    4.         people.Add ( new People ("Jacob", 27,false,false,"Support",boyCharacters[2]));
    5.         people.Add ( new People ("Tom", 25,false,false,"Support",boyCharacters[3]));
    6.  
    7.         people.Add ( new People ("Emma", 24,true,false,"Technical",girlCharacters[1]));
    8.         people.Add ( new People ("July", 24,true,false,"Support",girlCharacters[2]));
    9.         people.Add ( new People ("Timina", 24,true,false,"Web",girlCharacters[3]));

    How do I access this list in for example my Void Update in CreateNewPeople or in another script?
    I tried a lot of different thing, but none of them actually work.

    A extra question if anyone feels like answering that:
    How can I "cut" a person from the People List and "paste" the person into a completly different list, one for "PeopleThatAreHired".
     
  2. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    Your list called people can be accessed the same as any array:

    Code (CSharp):
    1. Debug.Log(people[0]) //<-this is the first entry in your list
    and, of course, that lets you iterate over it, like an array:

    Code (CSharp):
    1. foreach (People person in people)
    2. {
    3.      Debug.Log(person);
    4. }
     
    owyang and undevable like this.
  3. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    That would work inside of the start but in the Update I get this error:
    Because this is where I struggle. I don't understand why I can't access it in the Void Update, but it works fine in Start. Is it because I create the "new list" in start? but why doesn't it work to just add/remove/debug "people" in the void update?
     
  4. SkillBased

    SkillBased

    Joined:
    Aug 11, 2014
    Posts:
    141
    Ok, that's because of variable scope.

    Where you first declare your variables determines it's scope (from where else in your code the variable is useable).

    If you declare your variable in Start like this:

    Code (CSharp):
    1. void Start()
    2. {
    3.      List<People> people = new List<People>();
    4. }
    then that variable being delcare in the Start() method makes it local to that method. It cannot be accessed outside of it. In order for it to be accessed by all of your methods, the variable has to be declared first outside of any method. In other words, declared as a class-level variable, IE:

    Code (CSharp):
    1. class SomeClass : MonoBehaviour {
    2.  
    3.   List<People> people; //this makes the variable available throughout your class
    4.  
    5.   void Start()
    6.   {
    7.        people = new List<People>(); //here you can initialize its value in this method, but it remains a class-level variable
    8.   }
    9.  
    10.   void Update()
    11.   {
    12.        Debug.Log(people[0]);
    13.   }
    14. }
     
    Loff and GroZZleR like this.
  5. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Sounds like you're struggling with what's called "scope" in programming terms. I imagine your people list is declared locally to the Start() function and thus not available in the Update() function. You probably need to move the declaration of your list to the class level like this:

    Code (csharp):
    1.  
    2. class CreateNewList : MonoBehaviour
    3. {
    4.      public List<People> people;
    5.  
    6.      void Start()
    7.      {
    8.           people = new List<People>();
    9.      }
    10.    
    11.     void Update()
    12.     {
    13.           // whatever you're trying to do in update with your people list
    14.     }
    15. }
    16.  
     
    Loff likes this.
  6. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Thanks for good answers. I understand what you guys mean and looking forward to test it when I get home from work!