1. Unity 5.6 is now released.
    Dismiss Notice
  2. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  3. 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
  4. 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
  5. The launcher is currently pulling earlier versions of the content packages which may have errors, see post.
    Dismiss Notice
  6. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  7. Check out all the fixes for 5.5 in patch releases 1 & 2.
    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