Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Jan 04, 2016 at 09:11 PM by Le-Pampelmuse for the following reason:

The question is answered, right answer was accepted.

avatar image
1
Question by HueHue44 · Oct 16, 2015 at 10:32 PM · networkingnetworkserver

How to sync color with other players (Unet)

When a player is created he gets a random color, then I want all other players to sync with his color.

I can't get the network to sync the players colors! Either only one gets synced, no one gets synced or every player sees a random color on every player.

How do I sync the color between all player?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Oct 16, 2015 at 03:02 PM 0
Share

Post the code you have attempted

avatar image HueHue44 meat5000 ♦ · Oct 16, 2015 at 03:07 PM 0
Share

I have only tested around to try and understand, this one currently gets one synced but not the other ones ?

 [Command]
     void CmdSetColor(){
 
         Color myColor = new Color (Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f));
         
         RpcSetColor(myColor);
         
     }
 
 
     [ClientRpc]
     void RpcSetColor(Color col){
 
         GetComponent<Renderer> ().material.color = col;
 
     }

I call the CmdSetColor() in the start method if I am the local player

avatar image XiLiT HueHue44 · Mar 10, 2017 at 02:08 AM 0
Share

void Start(){ if(netId.Value==1) GetComponent ().material.color = Color.blue; if(netId.Value==2) GetComponent ().material.color = Color.red; if(netId.Value==3) GetComponent ().material.color = Color.green; //etc... }

avatar image XiLiT · Mar 10, 2017 at 02:06 AM 0
Share

void Start(){ if(netId.Value==1) GetComponent<$$anonymous$$eshRenderer> ().material.color = Color.blue; if(netId.Value==2) GetComponent<$$anonymous$$eshRenderer> ().material.color = Color.red; if(netId.Value==3) GetComponent<$$anonymous$$eshRenderer> ().material.color = Color.green; //etc... }

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by HueHue44 · Oct 18, 2015 at 06:06 PM

I solved it myself, if anyone else is having trouble with this here it is

 [SyncVar]private Color randomColor;
 
 void Start(){
 
 if(isLocalPlayer){
 randomColor = new Color (Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f), Random.Range (0.0f, 1.0f));
             GetComponentInChildren<Renderer>().material.color = randomColor;
 }
 
 }
 
 [Command]
 void Cmd_ProvideColorToServer(Color c){
 
 randomColor = c;
 }
 
 [ClientCallback]
 void TransmitColor(){
 if(isLocalPlayer){
 Cmd_ProvideColorToServer(randomColor);
 }
 }
 
 public override void OnStartClient ()
     {
         StartCoroutine (UpdateColor (1.5f));
 
     }
 
     IEnumerator UpdateColor(float time){
     
         float timer = time;
 
         while (timer > 0) {
             timer -= Time.deltaTime;
 
             TransmitColor ();
             if(!isLocalPlayer)
                 GetComponentInChildren<Renderer>().material.color = randomColor;
 
         
             yield return null;
         }
         
     
     }
 

I really feel like this isn't the best way to do it, I just kind of guessed away and eventually got the results I wanted. I only update the color for a short time after OnStartClient() because the color wont update if I just run it once in the OnStartClient() for some reason. Even though this works for me I would like to get some tips on how to improve this!

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rarceth · Oct 25, 2015 at 05:35 PM 0
Share

Hi, thanks for putting this up! i have just started working with both UNet, and multiplayer in general as part of my uni studies, and this has had me stumped for the better part of a week. One thing however, the result i am recieving is making the local player always black, yet for everyone else, the same color. eg, with 3 people, on the players screen, they are black, yet for the other two people, you are (for example) purple. Any ideas? (sorry to reopen, i saw this was only said to be "done" 10 days ago.)

avatar image AlphaSilverback · Aug 19, 2016 at 10:18 PM 1
Share

If you're still interested, I made an implementation that looks a bit like yours. It's a little bit cleaner and relies on the server to pick the color ins$$anonymous$$d of the Local Player instance. The Server then outputs this color to the other clients:

 public class NetworkRandomColor : NetworkBehaviour {
 
     // This list can be filled from the editor with multiple renderers in a prefab
     public List<Skinned$$anonymous$$eshRenderer> $$anonymous$$eshes = new List<Skinned$$anonymous$$eshRenderer>();
 
     [SyncVar]
     public Color m_color;
 
 
 
     void Start()
     {
         if ( isLocalPlayer)
         {
             
             foreach (Skinned$$anonymous$$eshRenderer mesh in $$anonymous$$eshes)
             {
                 mesh.material.color = m_color;
             }
             CmdSet$$anonymous$$eshColors(m_color);
         }
         else
         {
             CmdSet$$anonymous$$eshColors(m_color);
             foreach (Skinned$$anonymous$$eshRenderer mesh in $$anonymous$$eshes)
             {
                 mesh.material.color = m_color;
             }
         }
 
     }
 
     public override void OnStartClient()
     {
         
         if (isServer)
         {
             m_color = new Color(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f));
             RpcSetColor(m_color);
         }
         
     }
 
     
 
 
     [ClientRpc]
     void RpcSetColor( Color c )
     {
         m_color = c;
         foreach (Skinned$$anonymous$$eshRenderer mesh in $$anonymous$$eshes)
         {
             mesh.material.color = m_color;
         }
     }
 
 
     //Server Commands
     [Command]
     public void CmdSet$$anonymous$$eshColors(Color c)
     {
         m_color = c;
     }
 }
avatar image EXHOUL AlphaSilverback · Nov 24, 2020 at 07:27 PM 0
Share

Thank you so much!! After testing thousands of ways, this is what I find the best and most understandable solution. God bless you

avatar image the_nacho · Oct 08, 2016 at 11:08 PM 0
Share

I had a similar problem, and have a solution simpler than the ones that have been posted already. I wanted to share it in case it's helpful for someone (it's for 2D but could be changed to 3D fairly easily):

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.Networking;
 
 /// <summary>
 /// Assigns a random color to this sprite when it is created.
 /// </summary>
 public class RandomizeColor : NetworkBehaviour {
 
     /// <summary>
     /// The chosen color.
     /// </summary>
     [SyncVar]
     private Color chosenColor;
 
     void Start() {
         SetColor(chosenColor);
     }
 
     public override void OnStartClient() {
         if (isServer) {
             chosenColor = ChooseColor();
         }
     }
 
     /// <summary>
     /// Returns a random color.
     /// </summary>
     /// <returns>A random color.</returns>
     private Color ChooseColor() {
         // create a list of colors
         List<Color> colors = new List<Color> { Color.blue, Color.cyan, Color.green, Color.magenta, Color.red, Color.white, Color.yellow };
         // get a random color
         return colors[Random.Range(0, colors.Count)];
     }
 
     /// <summary>
     /// Sets the color of this sprite to the provided color.
     /// </summary>
     /// <param name="color">The color to set.</param>
     private void SetColor(Color color) {
         GetComponent<SpriteRenderer>().material.color = color;
     }
 }
avatar image $$anonymous$$ the_nacho · Sep 02, 2018 at 02:51 PM 0
Share

maybe i am a bid late but your code only work if you are the host do you know how i can fix that?

avatar image XiLiT · Mar 10, 2017 at 02:07 AM 0
Share

void Start(){ if(netId.Value==1) GetComponent ().material.color = Color.blue; if(netId.Value==2) GetComponent ().material.color = Color.red; if(netId.Value==3) GetComponent ().material.color = Color.green; //etc... }

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PLS recommend plugins for networking/sever which ONLY work for authentication + storing player info. 0 Answers

Yield to see if NetworkServer sends back anything? 0 Answers

Basic unity networking question 1 Answer

Connecting SQL with Unity 0 Answers

Warning: not all old messages was acknowledged 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges