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

4.6 Canvas UI Button Custom Script Function not showing in OnClick Inspector Dropdown

Discussion in 'UGUI & TextMesh Pro' started by chefdesanto, Oct 10, 2014.

  1. chefdesanto

    chefdesanto

    Joined:
    Sep 13, 2014
    Posts:
    4
    I am just leaning Unity, and upgrading an older database login script that used OnGUI before, to use the 4.6 Canvas elements instead.

    I am looking to have the OnClick of the UI Button call the function void ClickLoginButton() in the custom script titled LoginMenu

    the OnClick box in the inspector of the UI Button shows the name of my custom script, but doesn't list any functions

    (sorry just saw to use the developer preview forum for this)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System;
    5.  
    6. public class LoginMenu : MonoBehaviour {
    7.  
    8.     private string loginURL = "http://xxx.xxx.xxx.xxx/login.php";
    9.  
    10.     public static string username = "";
    11.     private string password = "";
    12.     private string label = "";
    13.  
    14.  
    15.     void ClickLoginButton(){
    16.         InputField username = GameObject.Find("usernameInput").GetComponent<InputField>();
    17.         string usernameSend = username.value;
    18.         InputField password =  GameObject.Find("passwordInput").GetComponent<InputField>();
    19.         string passwordSend = password.value;
    20.         StartCoroutine(HandleLogin(usernameSend, passwordSend));
    21.     }
    22.  
    23.     IEnumerator HandleLogin(string username, string password){
    24.  
    25.         string loginURL = this.loginURL + "?username=" + username + "&password=" + password;
    26.         WWW loginReader = new WWW (loginURL);
    27.         yield return loginReader;
    28.  
    29.         if(loginReader.error != null) {
    30.             label = "Error Connecting to Database Server.";
    31.         } else {
    32.             if(loginReader.text == "success"){
    33.                 label = "Successfully logged in.";
    34.                 Application.LoadLevel("Lobby");
    35.             } else {
    36.                 label = "Invalid Username or Password.";
    37.         }
    38.         }
    39.     }
    40. }
     
  2. CalaveraX

    CalaveraX

    Joined:
    Jul 11, 2013
    Posts:
    143
    If you dont specify that a method is public its automatically assumed that its private (the same happens with properties) and a method in order to be showed to the event in must fullfill 3 states.
    It must be public,
    it must return void
    and it must recieve zero or one parameter.

    You should change void ClickLoginButton() for public void ClickLoginButton() and that should work :)
     
  3. meapps

    meapps

    Joined:
    May 21, 2013
    Posts:
    167
    other option would be you call the Events from the Code and add it to the public LoginMenu
     
  4. chefdesanto

    chefdesanto

    Joined:
    Sep 13, 2014
    Posts:
    4
    Thanks, that was what I was missing.
     
  5. JB0ne

    JB0ne

    Joined:
    Apr 14, 2016
    Posts:
    1
    Line endings could also be an issue. Could happen when you've just made your script.
    Just something I ran into