Players spawned double but why?

The whole answer can be found below.

Please note: The Photon forum is closed permanently. After many dedicated years of service we have made the decision to retire our forum and switch to read-only: we've saved the best to last! And we offer you support through these channels:

Try Our
Documentation

Please check if you can find an answer in our extensive documentation on PUN.

Join Us
on Discord

Meet and talk to our staff and the entire Photon-Community via Discord.

Read More on
Stack Overflow

Find more information on Stack Overflow (for Circle members only).

Write Us
an E-Mail

Feel free to send your question directly to our developers.

Players spawned double but why?

camouflaged_penguin
2021-09-22 07:01:26

Hi there. I reused a piece of code for some testing. But when two players join the game the player prefab model is spawned two times for both of them and i can't figure out why.

This is the Spawn Method:

[PunRPC]    
    void ImInGame()    
    {    
        playersInGame++;    

        if (playersInGame == PhotonNetwork.PlayerList.Length)    
            Debug.Log("HALT");    
            SpawnPlayer();    
    }    


    void SpawnPlayer()    
    {    
        Debug.Log("Player Spawning");    
        //spawns local player according to their actor number    
        int id = PhotonNetwork.LocalPlayer.ActorNumber;    
           

        GameObject playerObj = PhotonNetwork.Instantiate(playerPrefabPath, spawnPoints[(id == -1) ? 0 : id % spawnPoints.Length].position, Quaternion.identity);    

        // initialize player    
        playerObj.GetComponent<PhotonView>().RPC("Initialize", RpcTarget.All, PhotonNetwork.LocalPlayer);    
    }    

And here the initialize:

[PunRPC]    
    public void Initialize(Player player)    
    {    
        Debug.Log("Initialize");    
        id = player.ActorNumber;    
        Debug.Log(id);    
        photonPlayer = player;    

        GameplayManager.instance.players[id - 1] = this;    

        // initialize the health bar    
        //headerInfo.Initialize(player.NickName, maxHp);    


        if (player.IsLocal)    
            me = this;    
        else    
            rb.isKinematic = true;    
    }    

When i run it i see both players with ID 1 and 2

Comments

Tobias
2021-09-22 09:18:41

Why so complicated?

I mean: Are you sure you need a ImInGame RPC? There is a callback when players join. You'd only need an RPC to signal that you are ready and even then, it might be better to use a Custom Player Property (which is a value that gets synced with late joining players).

It also seems like your Initialize can be replaced with OnPhotonInstantiate, which is part of the PhotonNetwork.Instantiate workflow already.

Your RPC sounds as if it's called for any joining player. And for each join, you PhotonNetwork.Instantiate a networked object.

camouflaged_penguin
2021-09-22 10:26:30

The players should not join late but start at the same time. But it might be the case that i don't understand you correctly.

The Second Thing is: it seems so but i am not sure about it because i want them to spawn at different locations and i have to set a position for the instantiate workflow and that would spawn all objects at the same place? or just initialize it at one place and spawn it at another?

Don't i have to call it for every player to initialize them? because every player needs to be a networked object with its own prefab.

The objects are not duplicated when i switch the number of spawn points.

camouflaged_penguin
2021-09-22 10:45:20

Also i don't know how to use

PhotonNetwork.Instantiate("MyPrefabName", new Vector3(0, 0, 0), Quaternion.identity, 0);    

Correctly. From the example i don't know where to put it.

camouflaged_penguin
2021-09-22 11:12:50

so i was a bit confused but put it like this:

void SpawnPlayer()    
    {    
        //spawns local player according to their actor number    
        int id = PhotonNetwork.LocalPlayer.ActorNumber;    
          


        //GameObject playerObj = PhotonNetwork.Instantiate(playerPrefabPath, spawnPoints[(id == -1) ? 0 : id % spawnPoints.Length].position, Quaternion.identity);    


        // initialize player    
        PhotonNetwork.Instantiate("Player", spawnPoints[(id == -1) ? 0 : id % spawnPoints.Length].position, Quaternion.identity, 0);    
        //playerObj.GetComponent<PhotonView>().RPC("Initialize", RpcTarget.All, PhotonNetwork.LocalPlayer);    
    }    

it worked But it also broke my camera controller which could not follow the players around anymore.

Tobias
2021-09-22 14:41:32

You probably want to read the PUN Basics Tutorial and code-along. It handles camera and input setup for multiplayer and how to instantiate.

camouflaged_penguin
2021-09-23 06:39:29

Thx Tobias

Seems you are omnipresent here and that there is some hard work right now but i appreciate that you took some time to answer here.

I build my first try with a somewhat outdated tutorial it seems because some things have changed and i have to deep dive into that. Not sure right now if i should stick to Pun ore work with Bolt instead. As far as i understand i can test both products freely and Bolt has some more advanced features while Pun was the first photon product.

As my goal is to have the knowledge and tools for professional Multiplayer based games (but still not the scale where something like quantum is needed) would you recommend bolt over pun?

Tobias
2021-09-23 13:49:13

In that case, I would recommend Fusion. It's our upcoming networking solution, which works with MonoBehaviours and enables even top notch, competitive multiplayer gaming. It is what happens when a few clever guys (not me) work on networking approaches for a long time.

Both, Bolt and PUN 2, will be around for a while longer as LTS versions but won't get bigger updates anymore.

Back to top