3
\$\begingroup\$

I have a cube in my scene. I want to change its color and I wanna server and all players to see it in this color. I have attached script on the cube:

using UnityEngine;
using System.Collections;
using UnityEngine.Networking;

public class Cube : NetworkBehaviour
{
    [SyncVar(hook = "OnColorChanged")]
    public Color myColor;

    void OnColorChanged(Color value)
    {
        GetComponent<Renderer>().material.color = myColor;
        Debug.Log(value + "," + myColor);
    }

    void Update()
    {
        Debug.Log(myColor);
    }
}

I run GameObject.FindWithTag("cube").GetComponent<Cube>().myColor = Color.red;this in my host script when I want to change the color of the cube, but nothing happens. enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ I had some issues with SyncVars, mostly because I was using a type that wasn't supported by the attribute or because on compilation a UNET warning was triggered containing the error. \$\endgroup\$ Commented Mar 19, 2017 at 15:25

1 Answer 1

1
\$\begingroup\$

From the documentation on SyncVar:

  • These variables will have their values sychronized from the server to clients in the game that are in the ready state.

Its possible that you are applying the change on a client.

SyncVars only sync changes from the server to the clients. If you want to update a SyncVar from a client, make the client send a Command from the client to the server that updates the value.

\$\endgroup\$
8
  • \$\begingroup\$ Thank you for your answer. I apply the change in my host script, so from server to client. \$\endgroup\$ Commented Sep 11, 2016 at 14:55
  • \$\begingroup\$ I see. Where are you executing the code GameObject.FindWithTag("cube").GetComponent<Cube>().myColor = Color.red;? \$\endgroup\$ Commented Sep 11, 2016 at 15:07
  • \$\begingroup\$ In my host script \$\endgroup\$ Commented Sep 11, 2016 at 15:11
  • \$\begingroup\$ I mean in which gameObject is the script. Is it on the cube? The NetworkManager? \$\endgroup\$ Commented Sep 11, 2016 at 15:13
  • \$\begingroup\$ The network manager \$\endgroup\$ Commented Sep 11, 2016 at 15:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.