Hello
I’m currently working on a multiplayer game where I have some players and a ball I want to give randomly to a player. Now since the allocation needs to be random, the server has to chose the player and give it to each client. I do this in OnStartServer() but I only get one player when I search for my player objects with FindGameObjectsWithTag(“Player”). The player objects are auto generated for each player by the NetworkLobbyManager. The scene loads fine by the way, with all players and not only one. That’s why I think the OnStartServer() I call on the ball object (which is already in the scene unlike the player objects) is executed before all player objects are loaded.
Can somebody please explain me in what order all the objects/the functions (OnStartServer, etc.) are loaded/called?
Okay, second problem: When the server decides which player should have the ball (which is atm always the same because of the problem above), it saves it in a SyncVar with hook on a function that sets the parent for the ball. But this function is not called on clients (that are not hosts). When i give back the SyncVar in Update() it has a value (initially it is null). So why does the hook not trigger and execute the function as desired when it changes its value? Kinda confused by this one.
EDIT: When i change the SyncVar after the game started (when I pass the ball to another player) the hook works perfectly fine. Maybe the client ball objects are not loaded yet when the server ball object changes the SyncVar value which results in the hook function not being executed on the client objects? Any idea how I can handle this?
Another problem I have is that I want to reference a GameController object to the player object. I assign it in the OnClientStart() and OnServerStart() with FindGameObjectWithTag(“GameController”). This works when I host the game in the editor but throws “Object reference not set to an instance of an object” when I host the game with the standalone version. Does anybody have an idea what could be the cause of this problem?
As you see I’m slightly confused by when and how gameObjects and players get loaded on the server and the clients and how I should assign object references at runtime.
Thanks in advance for your help : )
Sincerely,
Simon
PS: Would it help if I’d add the relevant snippets from my code?