Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

My first task is making a main menu, it is not going well.

Discussion in 'Scripting' started by Dev_Peasant, Mar 24, 2015.

  1. Dev_Peasant

    Dev_Peasant

    Joined:
    Mar 22, 2015
    Posts:
    13
    I am using GUIText,

    void OnGUI() {

    }

    when I lookup GUIText in the documentation there is no example code.

    I sort of rely on example code at the moment since i am new to coding in unity.

    What I am trying to do is use OnHover for the mouse, change text color, and possibly add a sound.

    Than I need on click function which selects a scene by name.

    Can someone help?

    Love you all <3
     
  2. ivyroxy

    ivyroxy

    Joined:
    Mar 23, 2015
    Posts:
    38
    I assume you're using Unity's Legacy OnGUI in C#. In that case, you probably want Label:
    Code (CSharp):
    1.  GUI.Label(new Rect(10, 10, 100, 20), "Hello World!");
    Or, you could also use Box:
    Code (CSharp):
    1. GUI.Box(new Rect(0, 0, Screen.width, Screen.height), "This is a title");
    These can all hold text, which I assume is what you want for a main menu.

    Remember, Unity's Scripting API is entirely available Online:
    http://docs.unity3d.com/ScriptReference/GUI.html

    Lemme know if this helped :)
     
  3. shkar-noori

    shkar-noori

    Joined:
    Jun 10, 2013
    Posts:
    833
    Why not use new Unity UI found in 4.6+?
     
  4. Dev_Peasant

    Dev_Peasant

    Joined:
    Mar 22, 2015
    Posts:
    13
    Would it be better if I made some textures for buttons and used buttons instead of text for the menu? Since I am using text for buttons or trying to.
     
  5. ivyroxy

    ivyroxy

    Joined:
    Mar 23, 2015
    Posts:
    38
    Yes, if you are going for Buttons, use
    Code (CSharp):
    1. public Texture btnTexture;
    2.     void OnGUI() {
    3.         if (!btnTexture) {
    4.             Debug.LogError("Please assign a texture on the inspector");
    5.             return;
    6.         }
    7.         if (GUI.Button(new Rect(10, 10, 50, 50), btnTexture))
    8.             Application.LoadLevel(1);
    9. }
    That being said, if you use Unity 4.6+, learning the new UI system would save you a heap of trouble and would make designing a fancy main menu way simpler :)
     
  6. Dev_Peasant

    Dev_Peasant

    Joined:
    Mar 22, 2015
    Posts:
    13
    nothing seems to be working, what exactly do i need to attach the script to in the level. I am just using the example code from the documentation.
    Code (CSharp):
    1. /* Example level loader */
    2.  
    3.  
    4. // JavaScript
    5. function OnGUI () {
    6.     // Make a background box
    7.     GUI.Box (Rect (10,10,100,90), "Loader Menu");
    8.    
    9.     // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
    10.     if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
    11.         Application.LoadLevel (1);
    12.     }
    13.    
    14.     // Make the second button.
    15.     if (GUI.Button (Rect (20,70,80,20), "Level 2")) {
    16.         Application.LoadLevel (2);
    17.     }
    18. }
    19.  
    20.  
    21. //C#
    22. using UnityEngine;
    23. using System.Collections;
    24.  
    25. public class menu : MonoBehaviour {
    26.    
    27.     void OnGUI () {
    28.         // Make a background box
    29.         GUI.Box(new Rect(10,10,100,90), "Loader Menu");
    30.        
    31.         // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
    32.         if(GUI.Button(new Rect(20,40,80,20), "Level 1")) {
    33.             Application.LoadLevel(1);
    34.         }
    35.        
    36.         // Make the second button.
    37.         if(GUI.Button(new Rect(20,70,80,20), "Level 2")) {
    38.             Application.LoadLevel(2);
    39.         }
    40.     }
    41. }
    42.  
     
  7. Dev_Peasant

    Dev_Peasant

    Joined:
    Mar 22, 2015
    Posts:
    13
    Okay I guess I should use the new GUI, I will look around for documentation on it.
     
  8. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    The top part is javascript and the bottom is c# remove the code language which you aren't using.
     
  9. Dev_Peasant

    Dev_Peasant

    Joined:
    Mar 22, 2015
    Posts:
    13
    Derp sorry, I found a nice video explaing how to use the new UI, it seems like i got it from there.

    I tried watching the official documentation, which is probably more informative, but personally I find I learn better from random people sometimes.

    Thank you all, it is nice to see the unity community is active and helpful. :)
     
  10. ivyroxy

    ivyroxy

    Joined:
    Mar 23, 2015
    Posts:
    38
    I personally prefer Javascript, but if you put this code on your Main Camera, it should work :)
    Code (JavaScript):
    1. function OnGUI () {
    2.     GUI.Label (Rect (10,10,100,35), "Loader Menu");
    3.     if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
    4.         Application.LoadLevel (1);
    5.     }
    6.     if (GUI.Button (Rect (20,70,80,20), "Level 2")) {
    7.         Application.LoadLevel (2);
    8.     }
    9. }
    You're welcome! Glad to help people in need! :)
     
  11. Dev_Peasant

    Dev_Peasant

    Joined:
    Mar 22, 2015
    Posts:
    13
    yeah the new ui tools are nice, now I am trying to find tuts on how to use UMA to create a character customization page. hmmmph
     
  12. ivyroxy

    ivyroxy

    Joined:
    Mar 23, 2015
    Posts:
    38
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Something like this?



    The code is throwaway, let me know if you want a copy. Can't give you the models, but they are free on the asset store.

    There are also a couple of menu tutorials on my channel.