Why am I getting this error when everything seems to join properly?

"Failed to Instantiate prefab: Player. Client should be in a room. Current connectionStateDetailed: JoinedLobby"

I have my code like this for my menu:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;


public class Menu : MonoBehaviour
{
    public TMP_InputField usernameInput;
    public TMP_InputField roomText;


    RoomInfo[] rooms;


    private void Start()
    {
        PhotonNetwork.ConnectUsingSettings("0.1");
        rooms = PhotonNetwork.GetRoomList();
    }


    private void OnConnectedToMaster()
    {
        PhotonNetwork.JoinLobby(TypedLobby.Default);
        Debug.Log("Connected");
    }


    bool InArray(string txt, RoomInfo[] rooms)
    {
        foreach(RoomInfo i in rooms)
        {
            if(i.Name == txt)
            {
                return true;
            }
        }
        return false;
    }


    public void Join()
    {
        if(InArray(roomText.text, rooms) && usernameInput.text.Length >= 2)
        {
            PhotonNetwork.playerName = usernameInput.text;
            PhotonNetwork.JoinRoom(roomText.text);
            Debug.Log(roomText.text + " joined!");


            PhotonNetwork.LoadLevel("Lobby");
        }
        else if(!(usernameInput.text.Length >= 2))
        {
            return;
        }
        else
        {
            Create();
        }
    }


    public void Create()
    {
        if(!InArray(roomText.text, rooms) && usernameInput.text.Length >= 2)
        {
            PhotonNetwork.playerName = usernameInput.text;
            PhotonNetwork.CreateRoom(roomText.text);
            Debug.Log(roomText.text + " created!");
            PhotonNetwork.LoadLevel("Lobby");
        }
        else if(!(usernameInput.text.Length >= 2))
        {
            return;
        }
        else
        {
            Join();
        }
    }
}


And this for my lobby:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class LobbyManager : MonoBehaviour
{
    public GameObject playerPrefab;


    void Start()
    {
        PhotonNetwork.automaticallySyncScene = true;
        PhotonNetwork.Instantiate(playerPrefab.name, new Vector2(0, 0), new Quaternion(0, 0, 0, 0), 0);
    }
}


And it seems fine to me. I've compared it to old code I've used and nothing seemed off. How can I fix this?

Best Answer

  • KingsleyD
    KingsleyD
    Accepted Answer

    I fixed it. I just had to make a timer wait for 2 seconds an then instantiate

Answers

  • KingsleyD
    KingsleyD
    Accepted Answer

    I fixed it. I just had to make a timer wait for 2 seconds an then instantiate