- Home /
Material Data over Network (Multiplayer)
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.
using UnityEngine;
using UnityEngine.Networking;
public class PlayerMovementOnline : NetworkBehaviour
{
public Rigidbody rb;
private float forceSense = 10;
private float stuckSense = 15;
private bool inputRight = false;
private bool inputLeft = false;
private bool inputForward = false;
private bool inputBackward = false;
public Transform player;
public int color = 0;
// Start is called before the first frame update
void Start()
{
Debug.Log("Script PlayerMovementOnline Loaded Successfully");
if (isLocalPlayer)
{
color = Random.Range(1, 9);
switch (color)
{
case 1:
GetComponent<Renderer>().material.color = Color.red;
break;
case 2:
GetComponent<Renderer>().material.color = Color.blue;
break;
case 3:
GetComponent<Renderer>().material.color = Color.yellow;
break;
case 4:
GetComponent<Renderer>().material.color = Color.green;
break;
case 5:
GetComponent<Renderer>().material.color = Color.white;
break;
case 6:
GetComponent<Renderer>().material.color = Color.black;
break;
case 7:
GetComponent<Renderer>().material.color = Color.gray;
break;
case 8:
GetComponent<Renderer>().material.color = Color.cyan;
break;
case 9:
GetComponent<Renderer>().material.color = Color.magenta;
break;
}
}
}
}
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.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Multiplayer - Parent's children not syncing 0 Answers
How to sync non-player objects movement in multiplayer game? 0 Answers
Multiplayer problem: How can i figure out client lag in position updating calculated by the host? 0 Answers
How to test Unity's new networking layer in separate game instances? 0 Answers