Null Reference of Instantiated GameObject when in Offline Mode

Options
Hey there,

I call this code on my GameController in my Game Scene (after successfully joining a room, etc.)
void Start()
    {
        _localPlayer = PhotonNetwork.Instantiate(_playerPrefab.name, Vector3.zero, Quaternion.identity, 0).GetComponent<NetworkPlayer>(); 
    }

which works just fine. I get each NetworkPlayer script to raise an event back to the GameController using OnPhotonInstantiate so the GameController starts the game on the MasterClient once all players have instantiated.

The issue issue occurs when I do the above in OfflineMode. No idea why but for some reason _localPlayer is null. Even a reference to the GameObject returns null (I just Debug Logged this in an update loop on the GameController to check).

Perhaps I am not correctly storing references to instantiated local Player objects in the first place.

I managed to work around the problem by adding the following to the Callback of the event raised from OnPhotonInstantiate :
private void OnPlayerInstantiated()
    {
        _instantiatedPlayers++;
        if (_instantiatedPlayers == PhotonNetwork.CurrentRoom.MaxPlayers && PhotonNetwork.IsMasterClient)
        {
            GameObject localPlayerGO = PhotonNetwork.LocalPlayer.TagObject as GameObject;
            _localPlayer = localPlayerGO.GetComponent<NetworkPlayer>();
            photonView.RPC("RPC_StartGame", RpcTarget.All,  null);
        }
    }

This obviously doesn't work for Online mode as only the MasterClient sets their _localPlayer variable. But a combination of the two works but feels like a hack!

Thanks in advanced!

Comments