How to access members of a List

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:

namespace Units
{
    public abstract class PlayerUnit : Entity
    {
    public enum PlayerUnitActions : byte
        {
            Action1, Action2, Action3, ...etc.
        }
     }
}

It’s then used to set up a list of available actions in a child class of PlayerUnit:

namespace Units
{
    public class UnitCandidate : PlayerUnit
    {
        public UnitCandidate() : base ()
        {           
            var UnitActions = new List<PlayerUnitActions> ()
            {
                PlayerUnitActions.Action1
                PlayerUnitActions.Action2
                ...etc.
            };
        }
    }
}

I then create instances of Unit Candidate:

public class UnitGenerator : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        var Candidate1 = new UnitCandidate ();
        var Candidate2 = new UnitCandidate ();
        ...etc

        List<UnitCandidate> Candidates = new List<UnitCandidate> ();
        Candidates.Add (Candidate1);
        Candidates.Add (Candidate2);
        ...etc

        foreach (UnitCandidate i in Candidates)
        {
            Debug.Log ("Name: " + i.FirstName + " " + i.LastName);
         ... etc.

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.

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:

        public UnitCandidate() : base ()
        {       
            var UnitActions = new List<PlayerUnitActions> ()
            {
                PlayerUnitActions.Action1
                PlayerUnitActions.Action2
                ...etc.
            };
        }

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.

Is it just a plain old Microsoft intellisense failure?

This may help you with intellisense problems:

https://discussions.unity.com/t/778503

Also, in the code above, UnitActions is a private variable in the class constructor. Nobody outside of that ctor is gonna see it.

I’ve truncated the code examples a lot to save space, so UnitActions is previously defined in PlayerUnit as a public list:

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.

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

2 Likes

Ah, such a dumb mistake! Cheers PraetorBlue.

Once I got rid of var it was easy enough to use:

{
Debug.Log (x.ToString ());
}```
1 Like