Search Unity

Question Material Data over Network (Multiplayer)

Discussion in 'Multiplayer' started by RarePika, Jul 8, 2020.

  1. RarePika

    RarePika

    Joined:
    Nov 16, 2016
    Posts:
    4
    I'm working on my first game with online multiplayer using UNet. Right now it's very simple, each player spawns as a cube, which they are able to control. When each player spawns in, the cube that they are controlling is set to a random color. The problem I'm having is how do I transmit this color information of all cubes over the network to the other players, so it can update the color on their clients. Right now, all other cubes beside the one the local player is controlling appears red. Here's my code that controls the random cube color.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3.  
    4. public class PlayerMovementOnline : NetworkBehaviour
    5.  
    6. {
    7.     public Rigidbody rb;
    8.     private float forceSense = 10;
    9.     private float stuckSense = 15;
    10.     private bool inputRight = false;
    11.     private bool inputLeft = false;
    12.     private bool inputForward = false;
    13.     private bool inputBackward = false;
    14.     public Transform player;
    15.     public int color = 0;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         Debug.Log("Script PlayerMovementOnline Loaded Successfully");
    21.         if (isLocalPlayer)
    22.         {
    23.             color = Random.Range(1, 9);
    24.             switch (color)
    25.             {
    26.                 case 1:
    27.                     GetComponent<Renderer>().material.color = Color.red;
    28.                     break;
    29.                 case 2:
    30.                     GetComponent<Renderer>().material.color = Color.blue;
    31.                     break;
    32.                 case 3:
    33.                     GetComponent<Renderer>().material.color = Color.yellow;
    34.                     break;
    35.                 case 4:
    36.                     GetComponent<Renderer>().material.color = Color.green;
    37.                     break;
    38.                 case 5:
    39.                     GetComponent<Renderer>().material.color = Color.white;
    40.                     break;
    41.                 case 6:
    42.                     GetComponent<Renderer>().material.color = Color.black;
    43.                     break;
    44.                 case 7:
    45.                     GetComponent<Renderer>().material.color = Color.gray;
    46.                     break;
    47.                 case 8:
    48.                     GetComponent<Renderer>().material.color = Color.cyan;
    49.                     break;
    50.                 case 9:
    51.                     GetComponent<Renderer>().material.color = Color.magenta;
    52.                     break;
    53.             }
    54.         }
    55.     }
    56. }
    Again, with this code, all other cubes beside the one the local player is controlling appears red. How do I go about sending the color information of all player's cubes over the network? Thanks.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Use a SyncVar or ClientRPC to send the color integer from the server to all the clients. But what you should actually do is not start a new project with a network API deprecated 2 years ago and largely abandoned several years before that. Use an alternative 3rd party API (since there is currently no fully supported network API from Unity) and do whatever that API would have you do.
     
  3. RarePika

    RarePika

    Joined:
    Nov 16, 2016
    Posts:
    4
    Thank you for the advice. What API would you recommend me use?