Question by
ErayT · May 29, 2016 at 04:03 PM ·
networkingnullreferenceexceptionrpc
Null Pointer at RPC Call Unity Network
Guys,
In one of my ClientRpc attributed methods I get null reference exception for that method. The called methods and the stack is below
public class GameNetworkManager : NetworkManager {
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId) {
SpawnPoint spawnPoint = FindObjectOfType<SpawnManager>().PickSpawnPoint();
GameObject car = (GameObject)Instantiate(playerPrefab, spawnPoint.transform.position, Quaternion.identity);
NetworkServer.AddPlayerForConnection(conn, car, playerControllerId);
CarRaceGame game = FindObjectOfType<CarRaceGame>();
game.AddCar(car.GetComponent<Car>()); // this is where Rpc method is in
}
}
public class CarRaceGame : NetworkBehaviour {
....................
[Server]
public void AddCar(Car car) {
cars.Add(car);
if (cars.Count >= requiredCarAmount) {
RpcSetGameState(GameState.Running);
}
}
[ClientRpc]
public void RpcSetGameState(GameState state) {
currentState = state;
if (OnStateChanged != null) {
OnStateChanged.Invoke(state);
}
}
}
Stack Tree:
NullReferenceException: Object reference not set to an instance of an object
CarRace.Element.CarRaceGame.CallRpcSetGameState (GameState state)
CarRace.Element.CarRaceGame.AddCar (CarRace.Element.Car car) (at Assets/CarRaceGame.cs:29)
CarRace.Manager.GameNetworkManager.OnServerAddPlayer (UnityEngine.Networking.NetworkConnection conn, Int16 playerControllerId) (at Assets/GameNetworkManager.cs:17)
UnityEngine.Networking.NetworkManager.OnServerAddPlayerMessageInternal (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkManager.cs:832)
UnityEngine.Networking.NetworkConnection.HandleReader (UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:430)
UnityEngine.Networking.NetworkConnection.HandleBytes (System.Byte[] buffer, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:386)
UnityEngine.Networking.NetworkConnection.TransportRecieve (System.Byte[] bytes, Int32 numBytes, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:536)
UnityEngine.Networking.NetworkServer.OnData (UnityEngine.Networking.NetworkConnection conn, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:754)
UnityEngine.Networking.NetworkServer+ServerSimpleWrapper.OnData (UnityEngine.Networking.NetworkConnection conn, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:1842)
UnityEngine.Networking.NetworkServerSimple.HandleData (Int32 connectionId, Int32 channelId, Int32 receivedSize, Byte error) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServerSimple.cs:382)
UnityEngine.Networking.NetworkServerSimple.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServerSimple.cs:248)
UnityEngine.Networking.NetworkServer.InternalUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:706)
Comment
Your answer
Follow this Question
Related Questions
Photon RPC Parameter NullReferenceException 1 Answer
How to sync RPC function Photon Network? 1 Answer
Network commands sent from clients not working. 1 Answer
ClientRPC not called from command on client instance during Start() 0 Answers
Exiting a UNET lobby causes future ClientRPC calls to be ignored. 1 Answer