1. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  2. We're looking for feedback on Unity Starter Kits! Let us know what you’d like.
    Dismiss Notice
  3. We’re giving 2017.1 beta testers a chance to win t-shirts and a Nintendo Switch. Read more on the blog.
    Dismiss Notice
  4. Unity 2017.1 beta is now available for download.
    Dismiss Notice
  5. Unity 5.6 is now released.
    Dismiss Notice
  6. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  7. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  8. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice

Detecting when the other player presses a button

Discussion in 'Multiplayer Networking' started by deydera, Mar 19, 2017.

  1. deydera


    May 1, 2014
    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:

    Code (CSharp):
    1. public class Change : NetworkBehaviour {
    2.      public GameObject player; // reference to the other player
    3.      [SyncVar(hook="Activate")] bool activate=false;
    4.      void Start()
    5.      {
    6.          if(!isLocalPlayer)
    7.          {
    8.              //Destroy(this);
    9.              return;
    10.          }
    11.           if (this.name == "Red(Clone)")
    12.          {
    13.              player = GameObject.Find("Green(Clone)");
    14.          }
    15.          else
    16.          {
    17.              player = GameObject.Find("Red(Clone)");
    18.          }
    19.      }
    20.      [RPC]
    21.      void Activate(bool act)
    22.      {
    23.          activate = act;
    24.      }
    25.      void FixedUpdate()
    26.      {
    28.             if(Input.GetKey(KeyCode.E))
    29.          {
    30.              player.GetComponent<Change> ().activate = true;
    31.              player.transform.position = new Vector3 (20f, 20f);
    32.      }
    33.          else
    34.          {
    35.              player.GetComponent<Change> ().activate = false;
    36.           }
    39.      }
    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
  2. deydera


    May 1, 2014
    Can anyone give any hints please? I've been trying different methods with no success for over 2 weeks now
  3. MichalBUnity


    Unity Technologies

    May 9, 2016

    Looking at your code snippet i might have spotted 2 potential issues with it.

    Depending on how you spawn your players, the issue might be that when the LocalPlayer(Host) is spawned and it runs Start() it can't find the other player because its not spawned yet. That would explain why it works for the client but not the server.

    Secondly you cant move another player you don't have authority over due to how the API is designed. You can try to read up a bit of the concepts here https://docs.unity3d.com/Manual/UNetConcepts.html