In and Out

Options
I have two scenes. One for playing and the other for changing the look of a character.

When I leave the playing scene to my settings scene. I want to pull my player game object out of a room, make my changes; then add the player back into the scene.

I've tried different variations of this.
PhotonNetwork.DestroyPlayerObjects(PhotonNetwork.player);
        PhotonNetwork.LeaveRoom();
        PhotonNetwork.LeaveLobby();
        Application.LoadLevel("PlayerSettings");
This works properly, but when I try to come back into the same room.

I have tried just spawning the player and I have tried reconnecting to the entire lobby and room.

Either I get into the same room, but I don't see the other players, or they don't see me.

The other error is the console says that I am already in the room, but I'm not.

Here is my entire Network Manager
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class NetworkManager : Photon.MonoBehaviour
{

    [SerializeField]
    Text connectionText;

    [SerializeField]
    Transform[] spawnPoints;

    [SerializeField]
    Camera sceneCamera;

    [SerializeField]
    GameObject serverWindow;

    [SerializeField]
    InputField username;

    [SerializeField]
    InputField fullname;

    [SerializeField]
    InputField messageWindow;

    [SerializeField]
    Dropdown gender;

    private GameObject player;
    Queue<string> messages;
    int messageCount = 6;
    PhotonView pv;
    PlayerCls playerObj = null;
    bool connected = false;

    void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {

    }

    void OnLevelWasLoaded(int level)
    {
        if ((level == 0)&&(GameObject.Find("PlayerInfo")))
        {
            playerObj = GameObject.FindWithTag("PlayerInfo").GetComponent<PlayerInfo>().GetPlayer();
            //SetupNetwork();
            //JoinRoom();
            //OnJoinedRoom();
            StartSpawnProcess(0f);
            //connected = true;
        }
    }

    // Use this for initialization
    void Start()
    {
        SetupNetwork();
    }
    
    void SetupNetwork()
    {
        pv = GetComponent<PhotonView>();
        messages = new Queue<string>(messageCount);
        PhotonNetwork.logLevel = PhotonLogLevel.Full;
        PhotonNetwork.ConnectUsingSettings("0.1");
        StartCoroutine("UpdateConnectionString");
    }

    // Update is called once per frame
    IEnumerator UpdateConnectionString()
    {
        while (true)
        {
            connectionText.text = PhotonNetwork.connectionStateDetailed.ToString();
            yield return null;
        }
    }

    void OnJoinedLobby()
    {
        serverWindow.SetActive(true);
    }

    void OnReceivedRoomListUpdate()
    {
        RoomInfo[] rooms = PhotonNetwork.GetRoomList();
    }

    public void GoToSettingsClick()
    {
        PhotonNetwork.DestroyPlayerObjects(PhotonNetwork.player);
        PhotonNetwork.LeaveRoom();
        PhotonNetwork.LeaveLobby();
        Application.LoadLevel("PlayerSettings");
    }

    public void JoinRoom()
    {
        PhotonNetwork.player.name = username.text;
        PhotonNetwork.playerName = fullname.text;
        RoomOptions ro = new RoomOptions() { isVisible = true, maxPlayers = 20 };
        PhotonNetwork.JoinOrCreateRoom("HealthFair", ro, TypedLobby.Default);
    }

    void OnJoinedRoom()
    {
        serverWindow.SetActive(false);
        StopCoroutine("UpdateConnectionString");
        connectionText.text = "";
        StartSpawnProcess(0f);
    }

    void StartSpawnProcess(float respawnTime)
    {
        sceneCamera.gameObject.SetActive(true);
        StartCoroutine("SpawnPlayer", respawnTime);
    }

    IEnumerator SpawnPlayer(float respawnTime)
    {
        yield return new WaitForSeconds(respawnTime);
        int index = UnityEngine.Random.Range(0, spawnPoints.Length);
        string playerGender = "FemalePlayer";
        if (playerObj != null)
            gender.value = playerObj.GenderID;

        if (gender.value != 0)
            playerGender = "MalePlayer";

        //player = (GameObject)PhotonNetwork.Instantiate(playerGender, spawnPoints[index].position, spawnPoints[index].rotation, 0);
        
        if (playerObj != null)
        {
            string playerStr = JsonUtility.ToJson(playerObj);
            object[] playerLst = { playerStr };
            player = PhotonNetwork.Instantiate(playerGender, spawnPoints[index].position, spawnPoints[index].rotation, 0, playerLst);
        }
        else
        {
            player = (GameObject)PhotonNetwork.Instantiate(playerGender, spawnPoints[index].position, spawnPoints[index].rotation, 0);  
        }

        //ConfigurePlayer();
        player.GetComponent<PlayerNetworkMover>().RespawnMe += StartSpawnProcess;
        player.GetComponent<PlayerNetworkMover>().SendNetworkMessage += AddMessage;
        sceneCamera.gameObject.SetActive(false);
        AddMessage("Spawned Player: " + PhotonNetwork.player.name);
    }

    void AddMessage(string message)
    {
        pv.RPC("AddMessage_RPC", PhotonTargets.All, message);
    }

    [PunRPC]
    void AddMessage_RPC(string message)
    {
        messages.Enqueue(message);
        if (messages.Count > messageCount)
            messages.Dequeue();

        messageWindow.text = "";
        foreach (string m in messages)
            messageWindow.text += m + "\n";


    }
}

Comments

  • jeanfabre
    Options
    Hi,

    maybe you are over complicating things. When leaving the room, stay connected to Photon, there is no need to totally disconnect, simply stay in the lobby. I would keep a reference of the room, and then when the player is done editing its character, join that room again.

    Also, why are you dealing with the Queue manually? maybe this is affecting connection and information is getting lost as a result.

    Bye,

    Jean