Search Unity

Buttons not working.

Discussion in 'Scripting' started by GeekForta, Sep 23, 2014.

  1. GeekForta

    GeekForta

    Joined:
    Sep 21, 2014
    Posts:
    7
    Ok so I came across this nice youtube video, did everything right , then buttons aren't working.
    I don't have any errors, only one but you can clear it and it unsolvable.

    this is the script, c# :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Menu : MonoBehaviour {
    5.    
    6.     private string CurMenu;
    7.     // Use this for initialization
    8.     void Start () {
    9.        
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.        
    15.     }
    16.    
    17.     void ToMenu(string menu){
    18.                 CurMenu = menu;
    19.         }
    20.            
    21.             void OnGUI (){
    22.                 if(CurMenu == "Main")
    23.                     Main ();
    24.                 if(CurMenu == "Host")
    25.                     Host();
    26.             }
    27.            
    28.             private void Main(){
    29.                 if(GUILayout.Button("Host a match"))
    30.                     ToMenu("Host");
    31.             }
    32.            
    33.             private void Host(){
    34.                
    35.                
    36.             }
    37.         }

    If you can help , please :)
     
  2. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Where did you attach that script?
    Also, don't you need to create a rectangle or something to have the button appear?
     
  3. GeekForta

    GeekForta

    Joined:
    Sep 21, 2014
    Posts:
    7
    I just followed the instructions , If you know how to solve it , please help , you can experiment on unity , write the script in c# , then attach it on to the main camera and it should work , for me it doesn't ;(
     
  4. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    Are you seeing a button? Is that the complete code?
    If it is the complete code you missed to set your startvalue in CurMenu. Currently at the start its empty and no menu can get selected
    Code (csharp):
    1. void Start () {
    2. ToMenu("Main");
    3. }
    A possible error message would be that Your class name "menu" and a variable "menu" is the same. So change one of the two to something different.
     
  5. GeekForta

    GeekForta

    Joined:
    Sep 21, 2014
    Posts:
    7
    Ok , I'll try it!
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    I'm guessing CurMenu has no value to start with.

    Menu isn't the same as menu, so that's not an issue.
     
  7. GeekForta

    GeekForta

    Joined:
    Sep 21, 2014
    Posts:
    7
    Nope, doesn't work , if you could send me the script of what you meant , that'd be great Kogar
     
  8. GeekForta

    GeekForta

    Joined:
    Sep 21, 2014
    Posts:
    7
    I am still very new at this you if you could send me the edited script please, thanks :)
     
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Post your new code. And be more descriptive than "doesn't work"
     
  10. GeekForta

    GeekForta

    Joined:
    Sep 21, 2014
    Posts:
    7
    That's the thing , Idk where I messed up , Idk what to do I don't understand what you're talking. Just tell me where I have to edit the code
     
  11. Kogar

    Kogar

    Joined:
    Jun 27, 2013
    Posts:
    80
    Ok i tried the code and have seen the error message. Main() is a defined word which c#/Net is using.
    Otherwise only the startvalue was missing. I changed Main() to something different to remove the error.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Menu : MonoBehaviour {
    5.    
    6.    private string CurMenu;
    7.    // Use this for initialization
    8.    void Start () {
    9.      ToMenu("Main");
    10.    }
    11.    
    12.    // Update is called once per frame
    13.    void Update () {
    14.      
    15.    }
    16.    
    17.    void ToMenu(string menu){
    18.      CurMenu = menu;
    19.    }
    20.    
    21.    void OnGUI (){
    22.      if(CurMenu == "Main")
    23.        MainMenu ();
    24.      if(CurMenu == "Host")
    25.        Host();
    26.    }
    27.    
    28.    private void MainMenu(){
    29.      if(GUILayout.Button("Host a match"))
    30.        ToMenu("Host");
    31.    }
    32.    
    33.    private void Host(){
    34.      
    35.      
    36.    }
    37. }
     
  12. GeekForta

    GeekForta

    Joined:
    Sep 21, 2014
    Posts:
    7
    thanks mate :)