- Home /
 
Detecting when the other player presses a button
I am making a 2 player multiplayer game and I need to detect when the other player presses a button, and when he presses the button it needs to change my current position, however it doesn't work and i can't figure why.
Here is the script i wrote:
 public class Change : NetworkBehaviour {
 
     public GameObject player; // reference to the other player
 
 
     [SyncVar(hook="Activate")] bool activate=false;
 
 
     void Start()
     {
         if(!isLocalPlayer)
         {
             //Destroy(this);
             return;
         }
 
          if (this.name == "Red(Clone)")
         {
             player = GameObject.Find("Green(Clone)");
         }
         else
         {
             player = GameObject.Find("Red(Clone)");
         }
 
     }
 
     [RPC]
     void Activate(bool act)
     {
         activate = act;
     }
 
 
     void FixedUpdate()
     {
       
            if(Input.GetKey(KeyCode.E))
         {
             player.GetComponent<Change> ().activate = true;
             player.transform.position = new Vector3 (20f, 20f);
     } 
         else
         {
             player.GetComponent<Change> ().activate = false;
          }
             
        
     }
 
 
               The player seems to receive the button press only when it is received from the client, however the position is not updated. I would appreciate any help. Thanks
Answer by Joe-Censored · Mar 17, 2017 at 06:53 PM
It looks like you're trying to change a syncvar on a client. You should be changing them on the server, which then automatically propagate to any clients. You'll probably want to call a Command after your GetKey is true, and update activate on the server in that command.
I don't really see the point of your Activate hook either.
Could you help me by telling me how to write that? I have been watching tutorials and reading documentation for the past week and I can't seem to get my head around the syncvars, commands and RPCs, the rest of the networking works out ok
Your answer
 
             Follow this Question
Related Questions
Syncing SpriteRenderer Between Clients 0 Answers
Unity networking tutorial? 6 Answers
Controlling a network game? 0 Answers
Unity NetworkTransformChild - Syncing the Child of a Child 0 Answers