Local Player instantiation issues

Options
Hello,

I created two different scenes : my Photon setup scene (create/join a room) and my Game scene.
I would like to be able to join at multiple times my Game scene after returning to the setup scene (I have a menu in the game scene in which I can load the initial scene).
However, when I come back to my Game Scene my Local Player is instantiated twice.

I am using the scripts coming from the PUN2 Tutorial :
public class PlayerManager : MonoBehaviourPunCallbacks, IPunObservable
{
    public static GameObject LocalPlayerInstance;

    void Awake()
    {
        if (photonView.IsMine)
        {
            LocalPlayerInstance = this.gameObject;
        }
        DontDestroyOnLoad(this.gameObject);
    }

    void Update()
    {
        UnityEngine.SceneManagement.SceneManager.sceneLoaded += OnSceneLoaded;
    }

    void OnSceneLoaded(UnityEngine.SceneManagement.Scene scene, UnityEngine.SceneManagement.LoadSceneMode loadingMode)
    {
        this.CalledOnLevelWasLoaded(scene.buildIndex);
    }


    void CalledOnLevelWasLoaded(int level)
    {
        if ((LocalPlayerInstance != null) && (!Physics.Raycast(transform.position, -Vector3.up, 5f)))
        {
            Debug.Log("Entered");
            transform.position = new Vector3(0f, 5f, 0f);
        }
    }

    public override void OnDisable()
    {
        base.OnDisable();
        UnityEngine.SceneManagement.SceneManager.sceneLoaded -= OnSceneLoaded;
    }
and a GameManager Script :
public class GameManager : MonoBehaviourPunCallbacks
{
    public static GameManager Instance;
    void Start()
    {
        Instance = this;
        if (ProxyManager.LocalPlayerInstance == null)
        {
            Debug.LogFormat("We are Instantiating LocalPlayer from {0}", SceneManagerHelper.ActiveSceneName);
            PhotonNetwork.Instantiate("Player", new Vector3(0f, 5f, 0f), Quaternion.identity, 0);
        }
        else
        {
            Debug.LogFormat("Ignoring scene load for {0}", SceneManagerHelper.ActiveSceneName);
        }
    }
Could you help me ? Thanks for the answers :smile:

Comments