Search Unity

3D platformer tutorial GUI script.

Discussion in 'Immediate Mode GUI (IMGUI)' started by G-DayBruce, Apr 13, 2010.

  1. G-DayBruce

    G-DayBruce

    Joined:
    Mar 9, 2010
    Posts:
    5
    Is there a properly written script for the GUI section of the 3D platformer tutorial. I've nothing to compare it to. I think I'm close but am still getting error messages.
    New to JavaScript and have scripted, or noodled in some proprietary languages. So I'm not 100% confident.

    Any help would totally rock as I've been stuck in the GUI section for some time.

    :(
     
  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
  3. G-DayBruce

    G-DayBruce

    Joined:
    Mar 9, 2010
    Posts:
    5
    Thank you so much for the help and direction.

    Just in case I thought I'd upload my script and the error message is below.

    ArgumentException: You can only call GUI functions from inside OnGUI.

    The Script:

    Code (csharp):
    1. // Links the LerpzTutorialSkin asset
    2. var gSkin : GUISkin;
    3.  
    4. // our backdropimage goes in here
    5. var backdrop : Texture2D;
    6.  
    7. // if true, we'll display the "loading..." message
    8. private var isLoading = false;
    9.  
    10. // the backdrop
    11. var backgroundStyle : GUIStyle = new GUIStyle();
    12.  
    13. // code checks to see if we have a valid GUI Skin Object
    14. function OnGUI ()
    15. {
    16. if (gSkin)
    17.     GUI.skin = gSkin;
    18. else
    19.     Debug.Log("StartMenuGUI: GUI Skin object missing!");
    20.    
    21. if (GUI.Button( Rect( (Screen.width/2)-70, Screen.height - 160, 140, 70), "Play")) 
    22. {
    23. isLoading = true;
    24. Application.LoadLevel("TheGame"); // load the game level.
    25. }
    26.  
    27. if (!isWebPlayer)
    28. {
    29. if (GUI.Button( Rect( (Screen.width/2)-70, Screen.height - 80, 140, 70), "Quit"))
    30. Application.Quit();
    31. }
    32.  
    33. if (isLoading)
    34. {
    35. GUI.Label ( Rect( (Screen.width/2)-110, (Screen.height / 2) - 60, 400, 70),
    36. "Loading...", "mainMenuTitle");
    37. }
    38. }
    39.  
    40.  
    41. backgroundStyle.normal.background = backdrop;
    42.  
    43. GUI.Label ( Rect( (Screen.width - (Screen.height * 2)) * 0.75, 0, Screen.height * 2,
    44. Screen.height), "", backgroundStyle);
    45.  
    46. GUI.Label ( Rect( (Screen.width/2)-197, 50, 400, 100), "Lerpz Escapes",
    47. "mainMenuTitle");
    48.  
    49. var isWebPlayer = (Application.platform == RuntimePlatform.OSXWebPlayer ||
    50. Application.platform == RuntimePlatform.WindowsWebPlayer);
    51.  
    52. // Make the script also execute in edit mode
    53. @script ExecuteInEditMode()




    If anyone could check out my script I'd appreciate it.

    Thanks again!!!
     

    Attached Files:

  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The problem is that the closing curly brace just before
    Code (csharp):
    1. backgroundStyle.normal.background = backdrop;
    ...actually closes the OnGUI function. The remaining code executes outside that function, hence the error. You need to remove that brace and reposition it just before the line:-
    Code (csharp):
    1. var isWebPlayer = (Application.platform...
     
  5. G-DayBruce

    G-DayBruce

    Joined:
    Mar 9, 2010
    Posts:
    5
    You rock!

    GDB
     
  6. Bikebreck

    Bikebreck

    Joined:
    Aug 4, 2010
    Posts:
    112
    Can you put up the fixed script? If yes thanks so much!