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 How to access members of a List

Discussion in 'Scripting' started by Hoogley, Aug 1, 2020.

  1. Hoogley

    Hoogley

    Joined:
    Feb 15, 2019
    Posts:
    9
    Hi,

    I've tried to look this one up, however there seems to be some reason I can't get any of the code I've found to work, so... here I am.

    I'm trying to work out how to access members of a List. The example I'll use is a List built from enums which I'm trying to show on Debug.Log.

    The Enum list is set up as follows:
    Code (CSharp):
    1. namespace Units
    2. {
    3.     public abstract class PlayerUnit : Entity
    4.     {
    5.     public enum PlayerUnitActions : byte
    6.         {
    7.             Action1, Action2, Action3, ...etc.
    8.         }
    9.      }
    10. }
    It's then used to set up a list of available actions in a child class of PlayerUnit:
    Code (CSharp):
    1. namespace Units
    2. {
    3.     public class UnitCandidate : PlayerUnit
    4.     {
    5.         public UnitCandidate() : base ()
    6.         {          
    7.             var UnitActions = new List<PlayerUnitActions> ()
    8.             {
    9.                 PlayerUnitActions.Action1
    10.                 PlayerUnitActions.Action2
    11.                 ...etc.
    12.             };
    13.         }
    14.     }
    15. }
    I then create instances of Unit Candidate:
    Code (CSharp):
    1. public class UnitGenerator : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.         var Candidate1 = new UnitCandidate ();
    7.         var Candidate2 = new UnitCandidate ();
    8.         ...etc
    9.  
    10.         List<UnitCandidate> Candidates = new List<UnitCandidate> ();
    11.         Candidates.Add (Candidate1);
    12.         Candidates.Add (Candidate2);
    13.         ...etc
    14.  
    15.         foreach (UnitCandidate i in Candidates)
    16.         {
    17.             Debug.Log ("Name: " + i.FirstName + " " + i.LastName);
    18.          ... etc.
    19.    
    Now, this is where I've come unstuck. Anything I use to try and show the members of list i.UnitActions doesn't seem to work.

    Any suggestions? Thanks in advance.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Can you elaborate on what "not working" means? Are you getting a compile error? Are you getting an error when you run the game? Are you just not getting the log output you're expecting?

    One potential issue I'm seeing is that your list of actions just seems to be a local variable in the Constructor:
    Code (CSharp):
    1.         public UnitCandidate() : base ()
    2.         {      
    3.             var UnitActions = new List<PlayerUnitActions> ()
    4.             {
    5.                 PlayerUnitActions.Action1
    6.                 PlayerUnitActions.Action2
    7.                 ...etc.
    8.             };
    9.         }
    You'd need to make that a field of the UnitCandidate to make it last outside of the constructor method and to be able to expose it.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
  4. Hoogley

    Hoogley

    Joined:
    Feb 15, 2019
    Posts:
    9
    I've truncated the code examples a lot to save space, so UnitActions is previously defined in PlayerUnit as a public list:

    Code (CSharp):
    1. public List<PlayerUnitActions> UnitActions { get; set; }
    I've tried a few things which have given various reasons for not working. Again I left out examples to save space and avoid confusing the issue, since I've tried a lot of different options and they didn't work.

    Short version: mostly iterative for and foreach loops on i.UnitActions which have generated "no instance" errors in Unity.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    is the
    var UnitActions = new List<PlayerUnitActions> ()
    actually there? If so you're not modifying the list from PlayerUnit, you're modifying a local variable that will cease to exist at the end of the constructor's invocation. Get rid of
    var
     
    Hoogley and Kurt-Dekker like this.
  6. Hoogley

    Hoogley

    Joined:
    Feb 15, 2019
    Posts:
    9
    Ah, such a dumb mistake! Cheers PraetorBlue.

    Once I got rid of
    var
    it was easy enough to use:
    foreach(var x in i.UnitActions)
    {
    Debug.Log (x.ToString ());
    }
     
    PraetorBlue likes this.