Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

UI Buttons event triggers in multiplayer.

Discussion in 'Multiplayer' started by saadminhas101, Nov 29, 2015.

  1. saadminhas101

    saadminhas101

    Joined:
    Oct 31, 2015
    Posts:
    5
    Hi all,

    Having a bit of issue,
    I can't get onClick() public functions to send instructions to the server from a client.
    It works fine when i'm acquiring inputs from the GetKeyDown function, but I just cant get it to work in a UI, server/client environment.

    Help would be appreciated.

    Below is my code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5.  
    6. public class RPCscript : NetworkBehaviour {
    7.  
    8.  
    9. public Material mat;
    10.  
    11.     void Start ()
    12.     {
    13.     mat.color = Color.white;
    14.     if(isServer)
    15.         GetComponent<RPCscript>().enabled = false;
    16.     if(isClient)
    17.         GetComponent<RPCscript>().enabled = true;
    18.    
    19.     }
    20.  
    21.     void Update ()
    22.     {
    23.         if(Input.GetKeyDown (KeyCode.Q))
    24.             {
    25.             CmdBlackColor(Color.black);
    26.             CmdHello ();
    27.             }
    28.        
    29.         if(Input.GetKeyDown (KeyCode.E))
    30.             CmdRedColor(Color.red);
    31.     }
    32.    
    33.     [Command]
    34.     public void CmdBlackColor(Color color)
    35.     {
    36.         mat.color = color;  
    37.     }
    38.    
    39.     [Command]
    40.     public void CmdHello()
    41.     {
    42.     Debug.Log ("Button Pressed!");
    43.     }
    44.    
    45.     [Command]
    46.     public void CmdRedColor(Color color)
    47.     {
    48.         mat.color = color;
    49.     }
    50. }
    51.  
     
    Last edited: Nov 30, 2015
  2. saadminhas101

    saadminhas101

    Joined:
    Oct 31, 2015
    Posts:
    5
    Apologies for not uploading the code the correct way.
    Hope it is clear now.
     
  3. saadminhas101

    saadminhas101

    Joined:
    Oct 31, 2015
    Posts:
    5
    I have solved the issue,

    By nesting the CMD functions withing another public function.

    That way when a click on the UI button is detected. unity will run the public function first and then it will run the CMD function resting within the public function.

    Peace.