-2

I have followed this tutorial multiplayer fps serie from Expressed Unity especially this episode "https://youtu.be/j9PC9RhurRI?list=PLD4OdGjxbaByCEOH3fOJ4MgOdROHHBKUo" and i need some help with it.

I have followed the video till 23:30 and then all sort of things are broken. I get error saying "Can not Instantiate before client joined/created a room. State: Joining." and i dont know what i should to do.

I have checked all the codes and everything but for nothing. Do you have solution? I don't which code have the problem so i copy all three of the codes i have edited following this video.

MpManager script:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.SceneManagement;

public class MPManager : MonoBehaviourPunCallbacks
{

    public GameObject[] EnableObjectsOnConnect;
    public GameObject[] DisableObjectsOnConnect;

    // Start is called before the first frame update
    void Start()
    {
        PhotonNetwork.ConnectUsingSettings();
        //PhotonNetwork.ConnectToRegion("eu");
    }


    public override void OnConnectedToMaster()
    {
        foreach(GameObject obj in EnableObjectsOnConnect)
        {
            obj.SetActive(true);
        }
        foreach(GameObject obj in DisableObjectsOnConnect)
        {
            obj.SetActive(false);
        }
        Debug.Log("Connected to photon");
    }

    public void JoinFFA()
    {
        PhotonNetwork.AutomaticallySyncScene = true;
        PhotonNetwork.JoinRandomRoom();
    }

    public override void OnJoinRandomFailed(short returnCode, string message)
    {
        CreateFFA();
    }

    public void CreateFFA()
    {
        PhotonNetwork.AutomaticallySyncScene = true;

        RoomOptions ro = new RoomOptions { MaxPlayers = 10, IsOpen = true, IsVisible = true };
        PhotonNetwork.CreateRoom("defaultFFA", ro, TypedLobby.Default);

        SceneManager.LoadScene("FFA");
    }
}

Movement script:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon;
using Photon.Pun;

public class Movement : MonoBehaviourPun
{
    public KeyCode Left;
    public KeyCode Right;
    public KeyCode Forward;
    public KeyCode Backward;

    [SerializeField]
    private float MoveSpeed = 50;

    private Rigidbody body;
    private GameObject cam;

    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent<Rigidbody>();
        cam = gameObject.transform.GetChild(0).gameObject;
        if (photonView.IsMine)
        {
            cam.SetActive(true);
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (photonView.IsMine)
        {
            float x = Input.GetAxis("Mouse X");
            float y = Input.GetAxis("Mouse Y");

            if (Input.GetKey(Left))
            {
                body.AddRelativeForce(Vector3.left * MoveSpeed, ForceMode.Impulse);
            }

            if (Input.GetKey(Right))
            {
                body.AddRelativeForce(Vector3.left * -MoveSpeed, ForceMode.Impulse);
            }

            if (Input.GetKey(Forward))
            {
                body.AddRelativeForce(Vector3.forward * MoveSpeed, ForceMode.Impulse);
            }

            if (Input.GetKey(Backward))
            {
                body.AddRelativeForce(Vector3.forward * -MoveSpeed, ForceMode.Impulse);
            }
            gameObject.transform.Rotate(new Vector3(0, x, 0));
            cam.transform.Rotate(new Vector3(-y, 0, 0));
        }
    }
}

FFa script:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon;
using Photon.Pun;

public class FFA : MonoBehaviourPun, IPunObservable
{

    public float SpawnTime;
    float timer;
    bool HasPlayerSpawned = false;

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

    }

    // Update is called once per frame
    void Update()
    {
        timer += Time.deltaTime;
        if(timer >= SpawnTime)
        {
            if (!HasPlayerSpawned)
            {
                PhotonNetwork.Instantiate("Player", Vector3.zero, Quaternion.identity, 0);
                HasPlayerSpawned = true;
            }

            timer = 0;
        }
    }

    public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    {
        if(stream.IsWriting)
        {

        }else if (stream.IsReading)
        {

        }
    }
}

Sorry if I had typos my english not good.

Lapa
  • 1
  • 1
  • 3
  • No unnecessary external links must be used in questions and please only post the code that throws the error not all the scripts – Maghil vannan Jun 10 '20 at 08:18
  • Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your problem. – SternK Jun 10 '20 at 11:38
  • What is your exact issue? What did you try to solve it? What exactly didn't "work" as expected? Please don't link as to a post but reproduce the complete question here ... if for some reason the external link breaks your post here becomes useless – derHugo Jun 10 '20 at 14:38
  • Now the post is edited! – Lapa Jun 11 '20 at 16:08

1 Answers1

0

Solution found! i just made the spawntime to 1 second instead of 0 so the player had time to join room before instantiating.

Lapa
  • 1
  • 1
  • 3