Search Unity

Null reference exception when calling Remote Procedure Call

Discussion in 'Multiplayer' started by Dudledok, Jul 13, 2015.

  1. Dudledok

    Dudledok

    Joined:
    Oct 24, 2013
    Posts:
    110
    Below is my NetworkBehaviour script which is attached to a GameObject in the scene for all players and the TriggerGameStart is called just fine but calling RpcStartGame gives me a null reference exception (line 9) and I can't see why this might be when it's part of the same script.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class NetworkController : NetworkBehaviour
    6. {
    7.     public void TriggerGameStart()
    8.     {
    9.         RpcStartGame();
    10.     }
    11.  
    12.     [ClientRpc]
    13.     void RpcStartGame()
    14.     {
    15.         UIController.Instance.StartGame ();
    16.     }
    17. }
    Any suggestions will be greatly appreciated.
     
  2. TheDreamEXE

    TheDreamEXE

    Joined:
    May 14, 2015
    Posts:
    60
    This might sound a little silly, but have you tried adding

    if(!isServer)
    return;

    just before the RPC? Just a thought
     
  3. Dudledok

    Dudledok

    Joined:
    Oct 24, 2013
    Posts:
    110
    It should only be called by the server because that's determined in the script which calls TriggerGameStart, but I added that anyway. Now the null reference exception is triggered when doing
    Code (CSharp):
    1. if(!isServer)
    This would imply that the gameobject itself is null but then how do I manage to call TriggerGameStart in the first place?