Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Why do color changes of object occur on server but not on the client?

Discussion in 'Multiplayer' started by stockhouse50, Aug 1, 2017.

  1. stockhouse50

    stockhouse50

    Joined:
    Apr 9, 2015
    Posts:
    14
    I have two players using the same prefab. I spawn in a sphere (non-player object) using this code:
    sphere = Instantiate(sphereprefab, transform.position, Quaternion.identity) as GameObject;
    NetworkServer.Spawn(sphere);

    I have this code attached to the sphere (Script name is SphereProperties):
    [SyncVar(hook = "UpdateSphereColor")]
    Color spherecolor;

    public void UpdateSphereColor(Color spherecolor)
    {
    GetComponent<Renderer>().material.color = spherecolor;
    }

    Now when I move either player object through the sphere using the "IsTriggerEnter" method, I want to change the sphere to the color of that player and I want each player to see that color. However, it doesn't matter which player moves through the sphere, only the server player see's the color change. Below is the code that is attached to each player and right now I just pass in to Color.red just for testing purposes.

    void OnTriggerEnter(Collider col)
    {

    if(col.tag == "sphereflag")
    {
    GameObject.FindGameObjectWithTag("sphereflag").GetComponent<SphereProperties> ().UpdateSphereColor(Color.red);
    }
    }

    I thought syncvars send any changes from the server to the clients?
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    You are changing colors on server and client. Secondly, you are never applying the Hook value to your syncvar.
     
  3. stockhouse50

    stockhouse50

    Joined:
    Apr 9, 2015
    Posts:
    14
    Hi, but the color isn't changing on the client and only on the server. How do I apply the color change to the client as well?

    Also, I thought by calling the syncvar function with a color from each player I was applying the Hook value?
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    No. I honestly don't see the point in a syncvar for your case. I think changing the color in the OnTrigger is prolly easier. And if you need server only simulation. Command & RPC will probably do the trick.
     
  5. stockhouse50

    stockhouse50

    Joined:
    Apr 9, 2015
    Posts:
    14
    So in my OnTriggerEnter method (scripted attached to my player) , why doesn't this code work?

    void OnTriggerEnter(Collider col)
    {

    if(col.tag == "sphereflag")
    {
    GameObject.FindGameObjectWithTag("sphereflag").GetComponent<SphereProperties>().UpdateSphereColor(Color.red);
    }
    }

    I thought the client calls the spheres syncvar hook method passing the color red. Then the method runs and changes the color of the sphere for all the clients to see.

    How would I use Command and RPC inside the OnTriggerEnter?
     
  6. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    I don't know why it doesn't work. Debug it, Check if it's being invoked everywhere etc.

    And to your second question. http://lmgtfy.com/?q=Unet+command+rpc