How to spawn Netcode multiplayer player with a button instead of automatically?

I followed a tutorial to make basic multiplayer connections with relay (host has code and must be entered to play) but i want to make it so that, on connection, the player must choose a color (not worried about that right now) and clicking a spawn button to spawn in (what i am trying to figure out). How can I do this, and can someone really try to explain what each step means?

Hi @chewboiye1,

It might be easier to provide you with a working project that will give you example scripts you can look through to better understand the over-all process.

Attached is a fully working project (no relay added, but it does support both client-server and distributed authority topologies):
AttahablesAndPlayerSpawning.zip (559.9 KB)

When you first load the project, you will want to open the BootStrapScene and then look at the ExtendedNetworkManager:

You will notice there is a PlayerSpawnHandler component added to this GameObject that handles spawning the players via:

  • The NetworkManager player prefab property.
  • When a client connects (using both types of callbacks).
  • When an external action occurs (i.e. like pressing a button) ** what you want to do **

Looking at the ExtendedNetworkManager component in the inspector view, you will notice under the NetworkManager properties section that the Default Player Prefab property has no assigned player prefab:

This tells the NetworkManager that you will handle spawning the player prefab instances yourself.

You will also notice a helper script, SceneLoader, that provides you with one way you can handle managing scene loading when not connected to a network session (i.e. using the standard SceneManager) and for when you are connected to a network session (i.e. using NetworkSceneManager):

Opening one of the two scenes you can transition between (i.e. AttachableBehaviours1 or AttachableBehaviours2), expanding the Canvas, and then selecting SpawnButtonRoot, you will see there is a PlayerSpawnButtonHandler (which derives from NetworkBehaviour):

If you open this script file, you will see two methods of interest:

  • ButtonClick: What is invoked when the button is clicked.
  • PlayerSpawnRequestRpc: What is invoked by clients when using a client-server topology.
    • Since the server is the “spawn authority” when using a client-server topology, you need to have clients communicate they are ready to spawn to the server so they server can instantiate and spawn their player prefab.

You will see that the PlayerSpawnButtonHandler invokes the PlayerSpawnHandler.SpawnPlayer method:

The SpawnPlayer method uses the NetworkObject.InstantiateAndSpawn method to handle both instantiating and spawning the player prefab. You will notice that the isPlayerObject parameter is set to true and the client identifier for the client requesting to have their player spawned is included as well.

The example includes some basic starter components that should also be useful to look over for reference.

Additional information:
PlayerObjects and player prefabs

Let me know if this helps you accomplish your goal?