Multyplayer: create a function from a server to al clients without respawn

Hi everybody,
I’m stuck with a simple function that it’s similar to this example:

One gray cube in scene (not respawned just a single static cube in all clients player without controls, nothing at all)
a script that would say: if press spacebar (in the local machine) make the cube blue in all clients.

i’ve tried following the simple multyplayer tutorial messing up with [SyncVar] and [Command] or (isServer)(isLocal)… Now i’ve restarted from scratch and write few line of code and put it in a empty gameobject
(that is my player Prefab).

Please can you help me? :roll_eyes:

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

public class serverCtrl : NetworkBehaviour {

    public GameObject cube;

    // Use this for initialization
    void Start () {
        cube = GameObject.Find("Cube");
    }
   
    // Update is called once per frame
    void Update () {

        if (Input.GetKeyDown(KeyCode.Space))
        {
            changeColor();
        }
       
    }

    void changeColor()
    {
     cube.GetComponent<MeshRenderer>().material.color = Color.red;
    }
}

Use Attributes and understand how the client/server relationship works.

https://docs.unity3d.com/ScriptReference/Networking.CommandAttribute.html
https://docs.unity3d.com/ScriptReference/Networking.ClientRpcAttribute.html
https://docs.unity3d.com/ScriptReference/Networking.ServerAttribute.html

thank’s a lot! Seems that clientRpc it’s a good way :wink:

It’s work with [clientRpc] the object must be in Local player authority! Thank you

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

public class serverCtrl : NetworkBehaviour {

    public GameObject cube;
   
    // Use this for initialization
    void Start ()
    {
        cube = GameObject.Find("Cube");
    }
   
    // Update is called once per frame
    void Update () {

        if (Input.GetKeyDown(KeyCode.Space))
        {
            RpcchangeColor();
        }
       
    }
    [ClientRpc]//[ClientRPC] functions are called by code on Unity Multiplayer servers, and then invoked on corresponding GameObjects on clients connected to the server.
    public void RpcchangeColor()
    {
     cube.GetComponent<MeshRenderer>().material.color = Color.red;
    }
}