unity-game-enginecolorsnetwork-programmingunity-networking

Unity Network Material color change does not work


I have a player object that has: Network Identity and Network Transform. Also an object that has a NetworkManager. When I connect 2 PCs. I can see both players movements. That is fine. But There are two balls in my game. One Green and one Red.

Here is the code that changes the ball's color.

public Material Right = null;
     public Material Left = null;

      void OnCollisionEnter(Collision collider)
          { 
       if (collider.gameObject.name == "_RightBall")
             {  randomnumb = UnityEngine.Random.Range(0, 11);
                      if (randomnumb >= 5)
                      {
                          Right.color = Color.red;  
                     }
                      else
                      {
                          Right.color = Color.green;               
                     }
             }
       }

When player hits the ball it changes its color. It works fine for both players. But when a player change ball's color, it changes for himself. Not for other players. I want all players to see the same color when a player change ball's color. I tried to add Network Identity and checked the "Local Player Authority" to my balls. But it did not work.


Solution

  • Old Way

    RPC functions work perfectly for what you are talking about. You can send more than one var via RPC. It does not send over the variable name but it sends over its value.

    var ballcolor : int = 2;  
    

    if you send ballcolor , you are sending the number 2. on the other side you interet it.

    var ballcolor : int = 0;
    @RPC
    function WhatIsTheStatusOfBall( incomingBallColor : int){
       ballcolor = incomingBallColor;
    }  
    

    now ballcolor is 2.

    you can pass multiple things like

    @RPC
    function WhatIsTheStatusOfBall(BallLocation : Vector3, BallAngle : float, incTraveSpeed : float){  
    

    then you can use these var locally.

    Here is some more info on how to use it. http://unity3d.com/support/documentation/Components/net-RPCDetails.html


    In response to your comment:

    New Way

    [Command] and [ClientRpc] is the equivalent in the new Networking system.
    You can [Command] from clients up to the server and [ClientRpc] from the server down to all clients. http://docs.unity3d.com/Manual/UNetActions.html

    In addition, you can send messages to individual clients using the Send() function on the connectionToClient of a NetworkBehaviour. http://docs.unity3d.com/ScriptReference/Networking.NetworkConnection.Send.html