Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

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

Discussion in 'Scripting' started by luigis, Jun 27, 2017.

  1. luigis

    luigis

    Joined:
    Oct 30, 2013
    Posts:
    25
    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? :rolleyes:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class serverCtrl : NetworkBehaviour {
    7.  
    8.     public GameObject cube;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         cube = GameObject.Find("Cube");
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.         if (Input.GetKeyDown(KeyCode.Space))
    19.         {
    20.             changeColor();
    21.         }
    22.        
    23.     }
    24.  
    25.     void changeColor()
    26.     {
    27.      cube.GetComponent<MeshRenderer>().material.color = Color.red;
    28.     }
    29. }
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
  3. luigis

    luigis

    Joined:
    Oct 30, 2013
    Posts:
    25
  4. luigis

    luigis

    Joined:
    Oct 30, 2013
    Posts:
    25
    It's work with [clientRpc] the object must be in Local player authority! Thank you

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class serverCtrl : NetworkBehaviour {
    7.  
    8.     public GameObject cube;
    9.    
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         cube = GameObject.Find("Cube");
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.  
    19.         if (Input.GetKeyDown(KeyCode.Space))
    20.         {
    21.             RpcchangeColor();
    22.         }
    23.        
    24.     }
    25.     [ClientRpc]//[ClientRPC] functions are called by code on Unity Multiplayer servers, and then invoked on corresponding GameObjects on clients connected to the server.
    26.     public void RpcchangeColor()
    27.     {
    28.      cube.GetComponent<MeshRenderer>().material.color = Color.red;
    29.     }
    30. }