3

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.

Edric
  • 24,639
  • 13
  • 81
  • 91
Tayfur Gazioglu
  • 109
  • 1
  • 11
  • on the same way you share position of your two player to each other device, you must share the ball color with them and in update() check whether the color is changed or not. – dev-masih Nov 15 '15 at 05:16
  • I cant use the same method that I used for players. Also as I know network transform only checks position and rotation. I tried to change my player's color, It did not work. It is not a problem tho. I just need to share ball's color. I do not know how to do it. @MasihAkbari – Tayfur Gazioglu Nov 15 '15 at 05:36

1 Answers1

1

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

dev-masih
  • 4,188
  • 3
  • 33
  • 55
  • Thank you bro. It seems that is what I am looking for I have no idea how to use it but long nights wait for me :D . One more question: Can I use this in NetworkBehaviour or just MonoBehaviour. Because my script is using NetworkBehaviour right now. @MasihAkbari – Tayfur Gazioglu Nov 15 '15 at 06:16
  • @TayfurGazioglu ok I'm Updating my answer. – dev-masih Nov 15 '15 at 06:53