Global array list c#

Hello, I’m sorry I don’t quite know how to do code snippets here. I have two scripts, a general sort of one which manages an array list and then a second which I wish to access the array list from.

So in the manager script (called PlayerManager) I have:

public ArrayList playerList = new ArrayList();
public class PlayerEntry
{
	public string name;
	public NetworkPlayer player;
}

Then in my other script I wish to access this array list and add new entries. The following commands attempt to create a new entry to this array list.

PlayerManager.PlayerEntry newEntry = new PlayerManager.PlayerEntry();
newEntry.name = playerName;
newEntry.player = Network.player;
PlayerManager.playerList.Add(newEntry);

The first command works where the program is able to create the temporary entry. However, the error appears to be on the final line where I actually attempt to access the list itself.

The error code itself is:

error CS0120: An object reference is required to access non-static member `PlayerManager.playerList'

The unity script reference implies that this is how you access global variables from another function with script.variable. Does anyone know what I am doing wrong?

You need to declare playerList as static to access it as you describe:

public static ArrayList playerList = new ArrayList();

But it may make more sense to keep playerList as an instance variable, and instead access it from a PlayerManager instance. Assuming PlayerManager is a MonoBehaviour on some GameObject, it would look something like:

gameObject.GetComponent<PlayerManager>().playerList.Add(newEntry);

It all depends on how you have things setup.