- Home /
[Solved]Networking - Null Reference Exception
Hello, I am getting this weird nullreferenceexception, I have been trying for hours to fix it but it has been to no prevail! Anyway here is the error:
I'm pretty sure that there is a instance of the object since there is one created in the same function, here is the three snippets of code that link together to spawn my player(this is where it breaks).
void OnConnectedToServer(){
myPlayer = Network.player;
networkView.RPC("MakePlayer", RPCMode.Server, myPlayer);
}
[RPC]
void MakePlayer(NetworkPlayer thisPlayer){
Transform newPlayer = Network.Instantiate(player, transform.position, transform.rotation, 0) as Transform;
if(thisPlayer != myPlayer){
networkView.RPC("EnableCamera", thisPlayer, newPlayer.networkView.viewID);
} else {
EnableCamera(newPlayer.networkView.viewID);
}
}
[RPC]
void EnableCamera(NetworkViewID playerID){
GameObject[] players;
players = GameObject.FindGameObjectsWithTag("Player");
foreach(GameObject thisPlayer in players){
if(thisPlayer.networkView.viewID == playerID){
thisPlayer.transform.FindChild("Camera").GetComponent<Camera>().enabled = true;
thisPlayer.GetComponentInChildren<AudioListener>().enabled = true;
break;
}
}
}
The line 113 that the error message points to is the EnableCamera(newPlayer.networkView.viewID); part in the MakePlayer function.
Thanks and I look forward to hearing your earliest response.
SOLUTION
The problem was that the newPlayer was being stored as a transform when it should be stored as a GameObject.
Answer by NathanSix · Jan 31, 2015 at 07:52 PM
I would definitely go to your camera and confirm that it is tagged with "MainCamera" If this isn't the issue then I think this newPlayer variable may be empty.
Answer by boxing_rex · Jan 31, 2015 at 07:57 PM
The camera is tagged MainCamera, but how can newPlayer be empty when a transform is instantiated when the variable is created ?
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
(C#)Usage of own type returns NullReferenceException 1 Answer
Editor forgets elements of array after saving changes to code 0 Answers
Parameter becomes null through UNet Rpc 1 Answer
Multiple Cars not working 1 Answer