Sync complex variables and components over network
Hey guys. So here is my situation.
I am making a multiplayer cardgame. On the beginning of the game 4 cards get layed on the field. The cards face down at the beginning (the prefab looks this way). Then I want to flip them. To do this I call a method on the card that rotates it half way, then changes the sprite to show the face of the card and then rotates the rest of the way.
This worked great on singleplayer but now it doesnt. The cards spriterenderer doesnt get synced to the clients. Only on the server the cards actually flip. The cards and the object that spawns them, aswell as the object that flips the cards exist on the server. I can see the cards rotate and move on the clients correctly, since they have a networktransform.
I have tried [SyncVar] on the renderer... nothing
I have tried [ClientRpc] and change the renderer in such a call but again... nothing. In this case the cards sometimes even dissapeared.
I should also mention that the flip method is a coroutine. Dont know if that is important.
If anybody could help me, that would be nice :)
I have done some more testing and came to the conclusion that the cards just dont really exist on the clients. On the clients I just see the bare prefab with only the transform being synced (NetworkTransform). So how do I get the gameobjects to actually exist on the clients?
Actually I dont care if there are copies of the server objects on my clients. I just want my clients to be able to see changes done to the objects on the server. As I said neither the Spriterenderer nor changes to the scripts get synced to the clients. Why? ;_;
Common guys. Pls help. :/
Answer by meat5000 · Apr 04, 2016 at 11:01 AM
You are taking the wrong approach.
You should solve this using Analogues.
An analogue is like "Linking by sight".
Instead of trying to send complex data, simplify it and sync the simple data.
Essentially you only need to send a float for a rotation, for example and let the client handle the SpriteRenderer by using that rotation value.
In order to get the most from the UNet system, you must read the Manual from start to finish as it deals with important concepts throughout. It is impossible to work it out from the scripting reference.
http://docs.unity3d.com/Manual/UNet.html
You dont want the server to literally control each how each client works directly. That is like playing a game from someone elses machine. You dont want the whole game sent across the internet.
You want the server to give small ques. The client picks up on these and then performs the operations itself based on that.
Put it this way... Each client already has ALL the information it needs to process the game. Its identical on server and client (under default UNet setup). So theres no need to be sending so much information. Its already got it. Just use little controls (of your own design if need be) of simple types to make this happen. Analogues.
Minimising bandwidth is essential in multiplayer gaming.
So you are not trying to control each sprite renderer directly from the server. The server simply gives a number based on where it wants the thing to be and the client reads that number and responds to it.
Am I making sense here?
Your answer
Follow this Question
Related Questions
syncvar not changing after command 0 Answers
How to sync a sprite changed at runtime using UNET? 1 Answer
Unity Mirror OnStartServer spawn GameObjects 1 Answer
Network Attributes details 0 Answers
Chat 5.5 synclist Stuck 1 Answer