Setting Display Color

Nathan generated the color in OnServerAddPlayer and then passed it to the player where it was set.

I did it the other way; I just call player.SetDisplayColor(); with no parameters and generate the color in MyNetworkPlayer.SetDisplayColor();

That gave the same result. But I was wondering if one way was better than the other or if it doesn’t matter at all.

I tried this method as well, but I found that Player 1 (edit: correction, player 2) remained Color.black rather than receiving a random color. I reimplemented using Nathan’s approach and both players were properly randomized.

Any idea why this might be the case?

EDIT: Out of curiosity, I played with this a bit more. It was Player 2 that was remaining the default color, not player 2. Also, I was starting the server in the built version of the game and the client in unity, rather than the other way around. I’m assuming that this bug has something to do with SyncVar callbacks… which we’ll be learning about in the next video :wink:

For me, both players did receive a random color, so Im not sure why you get different results than I do. Regardless of the host is the Unity Editor, or the build, I get the proper behavior.

In fact, Ive gone above and beyond the section 1 lessons. I uploaded a server build of the application to an AWS EC2 server and build the client app for Android devices. Now I can ‘play’ rolling a ball with my wife on our respective phones. We each get a unique color and can see each other roll around. Very cool!

when setting the random color did you put Random.Range(0,1) or Random.Range(0f,1f) if you dont specify whether your using an int or a float, it will use int as a default.

Random Range always takes a float. So if you use 0 instead of 0f it will cast the integer into a float.

This topic was automatically closed after 13 days. New replies are no longer allowed.

Privacy & Terms