UNet Server-Client Command Functions Help
HI, Im creating a UNet Board Game thats based on manipulating path Game Objects. My goal is to place all of the Path Gameobjects (which have scripts attached to manage it) into the Servers paths[] array (of Paths which is the script attached). I want the Server to be the only one who manages the array and relay the information back to the client.
I have setup the Player Script that Calls a [Command] Function which populates the paths[] array with the Path Game Objects Components that are already in the server (there are 81 objects before the player joins). This appears to be populated correctly as I can see the values of the game object when called, and the Path Variables sync across all clients properly.
For some reason when I try to issue a [Command] to change the variables on a paths[] variable, if the Command wasnt called by the player who called the initial Command to populate that paths[] array, it would return a null reference error. Why is that? From my understanding, if the function is a [Command], it should be performed on the server. So when the Inital population is called as a command it should populate the server's paths[] array. How come it returns null references to the clients who didnt initiate that command? I have left 3 Blocks of code below. The first explains the scenario and outcome above, the 2nd is the paths[] populating Command, and the 3rd is the command that changes the value in one of the paths[]. I have spent days trying to understand why this occurs, please help me, ive checked everywhere with no explanation found!
//I have denoted the 2 connections as Host and Client, CmdSetupPaths sets up the paths (btn3) and CmdCheckPaths checks if the path exists
// [FunctionCall1] : [FunctionCall2] -> [Output of FunctionCall2]
// Host.CmdSetupPath : Host.CmdCheckPath -> True
// Host.CmdSetupPath : Client.CmdCheckPath -> False
// Client.CmdSetupPath : Host.CmdCheckPath -> False
// Client.CmdSetupPath : Client.CmdCheckPath -> True
[Command] private void CmdGetPathsFromGame(){
if(!isServer){
Debug.Log("Not the Server... Exiting");
return;
}
bool foundAllPaths = true;
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
string pathName = "Path " + i.ToString() + j.ToString();
paths[i,j] = GameObject.Find(pathName).GetComponent<Path>();
if(paths[i,j]==null)
foundAllPaths = false;
}
}
if(!foundAllPaths)
Debug.Log("FAILED TO FIND ALL PATHS");
else
Debug.Log("FOUND ALL PATHS");
}
[Command] public void CmdSetGenState(int i, int j, bool n){
if (!isServer){
Debug.Log("Not Server... Function Returned");
return;
}
Debug.Log("Changing Path: " + i + j + " to " + n);
paths[i,j].GenStateChange(n);
if(paths[i,j].hasGen == n)
Debug.Log("Change Successful");
}
}
Your answer
Follow this Question
Related Questions
What is difference between Network.InitializeServer and Unet. please help me 0 Answers
API for multiplayer game with authoritative server? 1 Answer
How to allow All clients to move blocks? 0 Answers
Destroy only player after disconnecting from server 0 Answers
multiplayer game -how to connect each game-object to specific network player 0 Answers