So I have followed quill18’s tutorial on multiplayer which worked perfectly. I then tried to add my own features, in this case, I added a myUnit. When I tested it , it works fine on the server side, but on the client it didn’t. Here’s a snippet
void Start () {
//Is this actually my own local PlayerObject?
if( isLocalPlayer == false){
return;
}
CmdSpawnMyUnit();
CmdSpawnMySphere();
}
public GameObject PlayerUnitPrefab;
public GameObject myUnit;
[Command]
void CmdSpawnMyUnit(){
//We are guaranteed to be on the server right now.
GameObject go = Instantiate(PlayerUnitPrefab);
//selectedUnits.Add(go);
myUnit = go;
//Now that the object exists on the server, propagate it to all
//the clients(and also wire up the Network Identity)
Debug.Log("Player Object :: --Spawning Unit");
NetworkServer.SpawnWithClientAuthority(go,connectionToClient);
Debug.Log("Player Object :: --Unit Spawned");
}
public GameObject spherePrefab;
public GameObject sphereUnit;
[Command]
void CmdSpawnMySphere(){
//We are guaranteed to be on the severe right now.
GameObject go = Instantiate(spherePrefab);
sphereUnit = go;
//Now that the object exists on the server, propagate it to all
//the clients(and also wire up the Network Identity)
Debug.Log("Player Object :: --Spawning Sphere");
NetworkServer.SpawnWithClientAuthority(go,connectionToClient);
//cam = go.GetComponent<Camera>();
Debug.Log("Player Object :: --Sphere Spawned");
//Debug.Log("cam = " + cam.name);
}
}
The error message says
“UnassignedReferenceException: The variable myUnit of PlayerObject has not been assigned.You probably need to assign the myUnit variable of the PlayerObject script in the inspector.”
myUnit was not assigned when played on a client but on the server it works how it is supposed to. Meanwhile, there is no problem with the sphere, I can control them both server and client side. Any help would be appreciated.