unity-game-enginephotonphoton-pun

Making photon pun game and it has player duplicating bug


I'm making Photon.Pun FPS Game. If the other player is already joining the room and I join the room, the other player is duplicating.

How do I fix this bug?

RoomManager script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using TMPro;
using Unity.VisualScripting;
using Photon.Realtime;

public class RoomManager : MonoBehaviourPunCallbacks
{

    public static RoomManager instance;

    public GameObject player;
    public GameObject playersWeapon;
    [Space]
    public Transform spawnPoint;

    public GameObject Connectingcam;

    public TextMeshPro placeholderText;
    

    public string nickname;

    private pauseScript pauseScreenScript;



    [Space]
    public GameObject nameUI;
    public GameObject connectUI;

    void Awake()
    {
        instance = this;
    }
    public void ChangeNickname(string n)
    {
        nickname = n;
    }

    public void JoinRoomButtonPressed()
    {
        nameUI.SetActive(false);
        connectUI.SetActive(true);
        if(nickname == "")
        {
            nickname = "이름없는사용자" + Random.Range(0, 99999).ToString();
        }
        Debug.Log("Connecting to Server");
        PhotonNetwork.ConnectUsingSettings();
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    public override void OnConnectedToMaster()
    {
        base.OnConnectedToMaster();
        Debug.Log("Connected to Server");
        PhotonNetwork.JoinLobby();
    }

    public override void OnJoinedLobby()
    {
        base.OnJoinedLobby();
        PhotonNetwork.JoinOrCreateRoom("test", null, null);
    }

    public override void OnJoinedRoom()
    {
        Connectingcam.SetActive(false);
        
        base.OnJoinedRoom();
        Debug.Log("Joined Room");
        SpawnPlayer();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public override void OnDisconnected(DisconnectCause cause)
    {
        base.OnDisconnected(cause);
        Debug.Log("Disconnected from Server");

    }

    public override void OnPlayerEnteredRoom(Player newPlayer)
    {
        base.OnPlayerEnteredRoom(newPlayer);
    }

    public void SpawnPlayer(){
        if(PhotonNetwork.IsConnected && PhotonNetwork.LocalPlayer.IsLocal)
        {
            Debug.Log("Spawning Player");
            GameObject _player = PhotonNetwork.Instantiate(player.name, spawnPoint.position, Quaternion.identity);
            _player.GetComponent<PlayerSetup>().IsLocalPlayer();
            _player.GetComponent<HP>().IsLocalPlayer = true;
            _player.GetComponent<voidDeath>().IsLocalPlayer = true;
            _player.GetComponentInChildren<Weapon>().IsLocalPlayer = true;
            _player.GetComponent<PhotonView>().RPC("SetNickname", RpcTarget.AllBuffered, nickname);
            PhotonNetwork.LocalPlayer.NickName = nickname;
            Debug.Log("Spawned Player");
        }

        
    }

    public void killPlayer()
    {

    }  
}

I tried to print the spawning player. But it prints only 1 thing. It looks like the problem is at Instantiate function..


Solution

  • I fixed my bug.

    It was at HP script, I had to change Destroy() to PhotonNetwork.Destroy().