- Home /
Cant transfer object references through the network?
I am really sorry for asking this here, but I can't find the error in my code for some time now. I get this error when somebody respawns(both the server host and client)
This is the error that I get:
NullReferenceException: Object reference not set to an instance of an object Spawning.Respawn () (at Assets/Scripts/Spawning.cs:47)
This is Spawning.cs:
using UnityEngine;
using System.Collections;
public class Spawning : MonoBehaviour {
private bool hasSpawned = false;
GameObject[] spawnPoints;
static public GameObject player;
private int RN;
void Start(){
if(!networkView.isMine){
enabled = false;
}
else{
spawnPoints = GameObject.FindGameObjectsWithTag("Respawn");
}
}
[RPC]
void Respawn(){
if(player != null){
Network.Destroy(player);
}
RN = Random.Range(0, spawnPoints.Length);
Network.Instantiate(Resources.Load("Helicopters/Helicopter"), spawnPoints[RN].transform.position, spawnPoints[RN].transform.rotation, 5);
}
void OnGUI(){
if(Network.isClient || Network.isServer){
if(hasSpawned == false && GUI.Button(new Rect(50f, 25f, 150f, 30f), "Spawn")){
Debug.Log("Spawning player...");
networkView.RPC("Respawn", RPCMode.AllBuffered);
hasSpawned = true;
}
if(hasSpawned == true && GUI.Button(new Rect(50f, 25f, 150f, 30f), "Respawn")){
Debug.Log("Respawning player...");
networkView.RPC("Respawn", RPCMode.AllBuffered);
}
}
}
}
Any ideas?
Answer by KMKxJOEY1 · Oct 15, 2014 at 06:42 PM
Either your spawnpoints are not set up properly or there is no prefab to load from at that resource location.
I think neither of these are true, because when there is only a server hosting player, everything works fine
@edve98: You think neither is true? To quote Under Siege 2: "Assumption is the $$anonymous$$OTHER of all f*** ups"
Since the error seems to come from your Network.Instantiate line there are only two possible reasons like $$anonymous$$$$anonymous$$$$anonymous$$xJOEY1 said.
Either
there's no asset called "Helicopter" in the "Helicopters" folder inside a Resources folder
or your spawnpoints array contains null-elements or is itself null.
By looking at your RPC functions and your Start function it's quite easy to spot. You only initialize the spawnPoints array if this object is owned by you. However you send an RPC to all other players which will also call the Respawn method on their side (well, that's the point of a remote procedure call). This actually doesn't make any sense since you use Network.Instantiate which already uses an RPC to spawn the object on all clients. Calling Respawn on a foreign peer will of course fail since you don't initialized the spawnPoints array there.
You should recapitulate the whole respawn procedure and think about which method needs to be called where and by whom.
ps: since you have a player variable which probably holds a reference to the player object, which you see$$anonymous$$gly destroy before respawning, i just wonder if you actually set the new player object somewhere? $$anonymous$$aybe you want to assign the return value of Network.Instantiate to it?
@Bunny83 wow, now I can't believe I have actually done this, please convert your comment to answer.
P.s. I think I will write down the words "Assumption is the $$anonymous$$OTHER of all f*** ups" and I will always look at them when something goes wrong :P