I want to take the Players from a script where it is set like this:
public List<Player> Players = new List<Player>();
to a different script, how do I do it?
I want to take the Players from a script where it is set like this:
public List<Player> Players = new List<Player>();
to a different script, how do I do it?
You need a reference to the other script, usually from setting it in the Inspector where public fields are available for drag-n-drop.
public class FirstScript : MonoBehaviour
{
public List<Player> Players = new List<Player>();
}
public class OtherScript : MonoBehaviour
{
public FirstScript Reference; // assign this in the inspector
public void Start()
{
foreach (var x in Reference.Players)
{
Debug.Log(x);
}
}
}
it gives me This error on the semi-colon:
CS1519 Invalid token ‘;’ in class, record, struct, or interface member declaration
LaneFox posted two scripts in one code box which understandably confused you.
You already have your script containing your Players list and so you can reference the list from another script like this:
using UnityEngine;
public class Potato : MonoBehaviour
{
public PlayersScript ps; // Use the editor to drag the script that contains your Players list onto this field
public void Start()
{
foreach (var x in ps.Players) // Get each Player from the Players list
{
Debug.Log(x);
}
}
}
Note: Replace PlayersScript with the actual name of the script that contains your Players list.
oh ok tnx