1

I'm making a 2d game with unity and I want it to be multiplayer but the problem is when the player(who doesn't host the room) leaves and rejoins I get the error

NullReferenceException: Object reference not set to an instance of an object PlayerMovement.Start () (at Assets/Scripts/PlayerMovement.cs:22)

This is the code:

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


public class PlayerMovement : MonoBehaviour
{
        public MovementJoystick movementJoystick;
        public float playerSpeed;
        private Rigidbody2D rb;

        public Camera m_camera;

        PhotonView view;
    
        // Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody2D>();
    view = GetComponent<PhotonView>();
    movementJoystick = GameObject.FindGameObjectWithTag("JoyStick").GetComponent<MovementJoystick>();
    

    if (!view.IsMine)
    {
        m_camera.enabled = false;
    }
}

    
        // Update is called once per frame
        void FixedUpdate()
        {
            movementJoystick = GameObject.FindGameObjectWithTag("JoyStick").GetComponent<MovementJoystick>();

            if(view.IsMine){
            if(movementJoystick.joystickVec.y != 0)
            {
                rb.velocity = new Vector2(movementJoystick.joystickVec.x * playerSpeed, movementJoystick.joystickVec.y * playerSpeed);
            }
            else
            {
                rb.velocity = Vector2.zero;
            }
        }
        }
}

And this is the pause code:

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

public class PauseManager : MonoBehaviourPunCallbacks
{
 public GameObject pauseMenu;

    public bool isPaused;
    // Start is called before the first frame update
    void Start()
    {
        isPaused = false;
        pauseMenu.SetActive(false);    
    }

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

    }

    public void PauseButton()
    {

                PauseGame();
            
        }
    
    public void PauseGame(){
        pauseMenu.SetActive(true);

        isPaused = true;
    }

    public void ResumeGame(){
        pauseMenu.SetActive(false);
        
        isPaused = false;

    }



    public void QuitGame(){
        
        PhotonNetwork.LeaveRoom();
        PhotonNetwork.AutomaticallySyncScene = false;
        SceneManager.LoadScene("Lobby");
        pauseMenu.SetActive(false);
       
        
    }

         void OnApplicationPause ( bool pause )
     {
         if(pause){
            PauseGame();
         }
         
     }
}

tried looking through the code but I didn't find anything wrong.

KiynL
  • 4,097
  • 2
  • 16
  • 34
Coralap
  • 21
  • 2

0 Answers0