Adding object to a list does not work on non-host client
I'm making a grid-based turn-based strategy game with multiple units. Right now you can spawn units by pressing a key, which works fine. On the host client, spawning a unit adds that unit to the 'units' list, so that it can be selected/moved/etc.
However, if you spawn a unit on a non-host client, it's not added to the 'units' list for some reason. So, it is spawned, but it's impossible to select or move. Instead it just gives "NullReferenceException: Object reference not set to an instance of an object."
The list of units is in the Game Manager script:
public static GameManager instance;
public List<Unit> units = new List<Unit>();
There's a Player script which handles spawning:
[Command]
public void CmdSpawn()
{
obj = (GameObject)Instantiate(GameManager.instance.InfantryPrefab, transform.position, Quaternion.identity);
newInf = obj.GetComponent<Infantry>();
newInf.gridPosition = new Vector2(transform.position.x + Mathf.Floor(GameManager.instance.mapSize / 2), -transform.position.z + Mathf.Floor(GameManager.instance.mapSize / 2));
// spawn on the clients
NetworkServer.Spawn(obj);
// adding newInf to the list of units
GameManager.instance.units.Add(newInf);
}
public virtual void Update () {
if (Input.GetKeyDown(KeyCode.J) && isLocalPlayer)
{
CmdSpawn();
}
}
Is there a way to make it so the spawned units are added to the 'units' list on the non-host client as well?
Your answer
Follow this Question
Related Questions
Day/Night cycle turns black at night and how to Serialize it rightly? 0 Answers
Photon Networking - How to move all players from game scene to current room scene? 0 Answers
Calling Bool in Network Command Not Working 0 Answers
How to choose different teams one by one 1 Answer
Mobile Interaction 0 Answers