Help Spawning Multiple Players At Once

alonsogm4

New member
Hi there! I'm using UCC 2.2.8 with the latest version of both PUN2 and PUN2 Add-On.
I have the basic PUN logic working, where the players joins a room and can wait for others to do so before starting the game.
When the game starts and the scene changes, only one player gets its character instantiated, the rest remains as a static camera, but If I join after the game has started, my character is correctly instantiated so seems to be an issue to instantiate all of them at once.

I'm using the provided SingleCharacterSpawnManager as I don't need different characters for every player yet, is this the intended behavior of the script or I'm missing something? Any help is appreciated!
 

Justin

Administrator
Staff member
This normally happens if the remote PUN session hasn't loaded yet. Are you getting any errors? At this point the character controller hasn't initialized yet so it is just waiting for PUN. I'm not sure if there are more debugging tools available within PUN but this may be a question for them to see if there is anything that you can debug to determine why PUN hasn't finished loading.
 

alonsogm4

New member
This normally happens if the remote PUN session hasn't loaded yet. Are you getting any errors? At this point the character controller hasn't initialized yet so it is just waiting for PUN. I'm not sure if there are more debugging tools available within PUN but this may be a question for them to see if there is anything that you can debug to determine why PUN hasn't finished loading.
Thanks for your super-quick reply Justin!
Oh I see, maybe has something to do with PUN, I'm not getting any errors. I'll reach Photon support to get some more info on this, thanks again!
 

mxkacsa

New member
Hi!
I have the same problem.
Here is my solution until Photon not fix it.


C#:
using System.Linq;
using Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Game;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

public class CharacterSpawnFixer : MonoBehaviour
{
    [SerializeField]
    private SpawnManagerBase spawnManagerBase;

    private void Start()
    {
        if (!PhotonNetwork.IsMasterClient)
            return;

        Invoke(nameof(FixSpawn), 1f);
    }

    private void FixSpawn()
    {
        var characters = GameObject.FindGameObjectsWithTag("Player");
        Player[] characterPlayers = characters.Select(i => i.GetCachedComponent<PhotonView>()).Where(i => i != null).Select(i => i.Owner)
            .ToArray();
       
        var players = PhotonNetwork.PlayerList;

        foreach (var player in players)
        {
            if (characterPlayers.Where(i => i.UserId == player.UserId).ToArray().Length > 0)
            {
                Debug.Log("Player found, skip: " + player.NickName);
                continue;
            }

            Debug.Log("Player not found, spawn: " + player.NickName);
            spawnManagerBase.SpawnPlayer(player);
        }
    }
}
 

alonsogm4

New member
Hi!
I have the same problem.
Here is my solution until Photon not fix it.


C#:
using System.Linq;
using Opsive.UltimateCharacterController.AddOns.Multiplayer.PhotonPun.Game;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine;

public class CharacterSpawnFixer : MonoBehaviour
{
    [SerializeField]
    private SpawnManagerBase spawnManagerBase;

    private void Start()
    {
        if (!PhotonNetwork.IsMasterClient)
            return;

        Invoke(nameof(FixSpawn), 1f);
    }

    private void FixSpawn()
    {
        var characters = GameObject.FindGameObjectsWithTag("Player");
        Player[] characterPlayers = characters.Select(i => i.GetCachedComponent<PhotonView>()).Where(i => i != null).Select(i => i.Owner)
            .ToArray();
      
        var players = PhotonNetwork.PlayerList;

        foreach (var player in players)
        {
            if (characterPlayers.Where(i => i.UserId == player.UserId).ToArray().Length > 0)
            {
                Debug.Log("Player found, skip: " + player.NickName);
                continue;
            }

            Debug.Log("Player not found, spawn: " + player.NickName);
            spawnManagerBase.SpawnPlayer(player);
        }
    }
}
Thanks mxkacsa for sharing your custom fix for this annoying issue, it works like a charm!
 

psYos

New member
Hi!
Where did you attach this script? Up until now I had to comment out code in the spawnmanagerbase to get all players to spawn. I would like to uncomment that code and try this method.
 

mxkacsa

New member
You can put the CharacterSpawnFixer to everywhere.
You need to attach your spawnManager script (default is the "SingleCharacterSpawnManager" component in the PunGame gameobject)

Here is my Solution with custom spawn manager:
index.php
 

Attachments

  • help.png
    help.png
    51 KB · Views: 11
Top