Question about object referencing on instantiation [using Forge Networking]

Ok, so I’m currently using Forge Networking, and I’ve got an issue. The reason I’m posting it here is because I feel as though the issue is logic related, and not so much code related. So the code I have below is a network player spawner that creates a player 1 and player 2 object when they connect to the network. The problem is that I have a separate StateManager script that holds both instances of the players when they’re made and hold them for the duration of the game. It does so with a simple playerSpawn.GetComponent(), so nothing fancy. The code for the spawner looks like this:

using UnityEngine;
using BeardedManStudios.Network;

public class PlayerSpawn : NetworkedMonoBehavior {

	public Player player1Created;
	public Player player2Created;

	protected override void Start () {
		base.Start();
		if (Networking.PrimarySocket.Connected)
			if(OwningNetWorker.IsServer)
				Networking.Instantiate("Player1", NetworkReceivers.AllBuffered, callback: OnCreatePlayer1);
			else
				Networking.Instantiate("Player2", NetworkReceivers.AllBuffered, callback: OnCreatePlayer2);

		else
		{
			Networking.PrimarySocket.connected += delegate()
			{
				if(OwningNetWorker.IsServer)
					Networking.Instantiate("Player1", NetworkReceivers.AllBuffered, callback: OnCreatePlayer1);
				else
					Networking.Instantiate("Player2", NetworkReceivers.AllBuffered, callback: OnCreatePlayer2);
			};
		}
	}


	void OnCreatePlayer1(GameObject newObject)
	{
		player1Created = newObject.GetComponent<Player>();
	}

	void OnCreatePlayer2(GameObject newObject)
	{
		player2Created = newObject.GetComponent<Player>();
	}
}

What I’m noticing to be the issue is that this check to reference the players is only happening on one frame on the Start function when a player connects. The awake function doesn’t spawn any players, and the Update spawns and infinite amount, and it no longer stores the player info inside of my StateManager script if I set some boolean logic to stop spawning more than one of each player1 or player2 prefab. Any ideas on how to get BOTH players located safely on my StateManager? I don’t think the StateManager code is necessary, but I can post it on request. One last thing to note is that Network.Instantiate on Forge Networking returns void, so I had to use the callback methods shown below. Thanks for any input and ideas! Means a lot.

Feel free to join us on slack!

ForgeNetworking.slack.com

To get invited just register your invoice at developers.forgepowered.com/Profile. Where you would receive live support on these issues and more. :slight_smile:

We have a method “NetworkStart” which is a start function that is called once you have been setup on the network for this object. You could take advantage of that :slight_smile:

using UnityEngine;
using BeardedManStudios.Network;

public class PlayerSpawn : NetworkedMonoBehavior
{
	public Player player1Created;
	public Player player2Created;

	protected override void NetworkStart()
	{
		base.NetworkStart();
		
		if(OwningNetWorker.IsServer)
			Networking.Instantiate("Player1", NetworkReceivers.AllBuffered, callback: OnCreatePlayer1);
		else
			Networking.Instantiate("Player2", NetworkReceivers.AllBuffered, callback: OnCreatePlayer2);
	}

	void OnCreatePlayer1(GameObject newObject)
	{
		player1Created = newObject.GetComponent<Player>();
	}

	void OnCreatePlayer2(GameObject newObject)
	{
		player2Created = newObject.GetComponent<Player>();
	}
}