NullReferenceException error

Options
I am trying to use photonView.isMine, but I am getting this error:
NullReferenceException: Object reference not set to an instance of an object
MultiplayerHandler.OnJoinedRoom () (at Assets/Scripts/MultiplayerHandler.cs:20)

Here is my code
[code2=csharp]void OnJoinedRoom()
{
GameObject player = PhotonNetwork.Instantiate("ClientPlayer", Vector3.zero, Quaternion.identity, 0);
if (photonView.isMine)
{
(player.GetComponent("CharacterMotor") as MonoBehaviour).enabled = true;
(player.GetComponent("FPSInputController") as MonoBehaviour).enabled = true;
(player.GetComponent("MouseLook")as MonoBehaviour).enabled = true;
}[/code2]

Comments

  • Merrik
    Options
    Sounds like there isn't Photonview component on the object?
  • Tobias
    Options
    You don't want to check isMine on the "this" script but your instantiated object's photonView:
    player.isMine
  • Merrik wrote:
    Sounds like there isn't Photonview component on the object?

    Yes PhotonView is attached to the Game Object "ClientPlayer"
    Tobias wrote:
    You don't want to check isMine on the "this" script but your instantiated object's photonView:
    player.isMine

    My reason for trying to do this is that when I just do this:

    [code2=csharp]void OnJoinedRoom()
    {
    player = PhotonNetwork.Instantiate("ClientPlayer", Vector3.zero, Quaternion.identity, 0);
    (player.GetComponent("CharacterMotor") as MonoBehaviour).enabled = true;
    (player.GetComponent("FPSInputController") as MonoBehaviour).enabled = true;
    (player.GetComponent("MouseLook")as MonoBehaviour).enabled = true;

    }[/code2]

    like the Marco Polo tutorial says if I run two clients one controls the other but not itself and vice versa. I was trying to fix it with photonView.isMine, but maybe you can tell me why my problem might be happening.

    Also player.isMine gave me an error.
  • Tobias
    Options
    You're right: player.isMine is not working. The GameObject is not the PhotonView.
    But it has a component PhotonView. Try:
    [code2=csharp]PhotonView myPhotonView = (PhotonView)player.GetComponent<PhotonView>();
    if (myPhotonView.isMine) { //... your code[/code2]
  • Alright. Now what is happening one player will join the room with everything enabled, and then once another player joins the first player snaps to the position of the second player and they both get everything disabled. Except I can still move the second player through the first player's client.

    Here is the new code:
    [code2=csharp]void OnJoinedRoom()
    {
    GameObject player = PhotonNetwork.Instantiate("ClientPlayer", Vector3.zero, Quaternion.identity, 0);
    PhotonView myPhotonView = (PhotonView)player.GetComponent<PhotonView>();

    if (myPhotonView.isMine)
    {
    (player.GetComponent("CharacterMotor") as MonoBehaviour).enabled = true;
    (player.GetComponent("FPSInputController") as MonoBehaviour).enabled = true;
    (player.GetComponent("MouseLook")as MonoBehaviour).enabled = true;
    }

    }[/code2]

    Also here is a picture of how I have set up client player:

    1ik2rm.png
  • Tobias
    Options
    Maybe the scripts are enabled on the other prefabs?
  • From what I have seen, when the second player comes in nothing is enabled. Also I don't know if this will help this takes place from the first person perspective and the prefab "ClientPlayer" is just a first person controller.

    The only other thing I can show you are my two scripts relating to multiplayer.

    Main Menu script:

    [code2=csharp]public class MMManager : MonoBehaviour {
    //Variables



    // Use this for initialization
    void Start ()
    {
    PhotonNetwork.ConnectUsingSettings("v1.0");
    }

    // Update is called once per frame
    void Update ()
    {
    if(Input.GetMouseButtonDown(0) && PhotonNetwork.connected)
    {
    PhotonNetwork.CreateRoom("testRoom");
    Debug.Log("Created Room");
    PhotonNetwork.JoinRoom("testRoom");
    Debug.Log ("Joined Room");
    PhotonNetwork.isMessageQueueRunning = false;
    Application.LoadLevel("TestMap");
    PhotonNetwork.isMessageQueueRunning = true;


    }
    }[/code2]


    And the script I have been showing you. I have modified it a little bit with the same result:

    [code2=csharp]public class MultiplayerHandler : Photon.MonoBehaviour {


    // Use this for initialization
    void Start ()
    {

    }

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

    }
    void OnJoinedRoom()
    {
    GameObject player = PhotonNetwork.Instantiate("ClientPlayer", Vector3.zero, Quaternion.identity, 0);
    PhotonView myPhotonView = (PhotonView)player.GetComponent<PhotonView>();

    if (myPhotonView.isMine)
    {
    CharacterController controller = player.GetComponent<CharacterController>();
    controller.enabled = true;
    (player.GetComponent("CharacterMotor") as MonoBehaviour).enabled = true;
    (player.GetComponent("FPSInputController") as MonoBehaviour).enabled = true;
    (player.GetComponent("MouseLook")as MonoBehaviour).enabled = true;
    }

    }


    }[/code2]
  • Tobias
    Options
    Your MMManager is moving a char by input. This script can only be active once (on your local instance) but I don't see you deactivate it.
    From the scripts, I don't see why everything is mixed up.

    In cases like this, I like to start from scratch in a new project. Try to get the minimum of scripts working (maybe even using the tutorial as guide). Then add script by script of your "original" project and see how it changes behaviour. If something breaks, the cause for that can be found in the latest changes and be isolated.
  • Can do. I'll come back and tell you my findings. Thanks for your help so far.
  • Alright I am back. The problem starts once I add this code from the tutorial:
    [code2=csharp]CharacterMotor characterController = player.GetComponent<CharacterMotor>();
    characterController.canControl = true;
    MouseLook cameraScript = player.GetComponent<MouseLook>();
    cameraScript.enabled = true;[/code2]

    If I just instantiate the player then both can move, but once I add this I have the problem of player one controlling player 2 but not itself, and vice versa happens. Does that help you solve the problem? I also tried checking if Photonview was mine but got the same result.
  • bump
  • Tobias
    Options
    It doesn't really help, no. I can't analyze all your scripts and solve the issue for you, sorry.

    If your input is used by some scripts on a character and moves that char even if it's not yours, you can: disable that script on that char or ignore the input by checking pv.isMine. The isMine check means each client instantiates it's own char.
    If player 1 controls player 2's char and vice versa, you are maybe doing the opposite of what you wanted. Like: You maybe enable a script on objects you didn't instantiate or it might be enabled anyways.
  • Don't worry about it. Thanks for the help anyway. I have decided to step away from the networking aspect of my game. Hopefully when I return to it I will be a more proficient coder and be able to use Photon.
  • Tobias
    Options
    Hm.
    Ok, maybe this is a good thing.
    Wishing you the best!