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. Dismiss Notice

GUILayout Help needed

Discussion in 'Immediate Mode GUI (IMGUI)' started by sbroscious, Jun 25, 2014.

  1. sbroscious

    sbroscious

    Joined:
    Jun 25, 2014
    Posts:
    4
    I have recently gotten into coding my own games instead of just using the assets of others.

    While working through the examples at wiki.unity3d.com I've come across issues (that I've attributed to my using the free version of Unity) which I've been able to remedy on my own, however, I cannot wrap my head around why the following code isn't working:
    Code (csharp):
    1.  
    2. {
    3.     public string message = "Hello World!";
    4.     void OnGui()
    5.     {
    6.         GUILayout.Label (message);
    7.     }
    8.  
    9. }
    10.  
    Can anyone please inform me as to what this code is missing in order to achieve the same effect found in the image at http://docs.unity3d.com/ScriptReference/GUILayout.Label.html

    Thanks in advance for any and all help!
     
  2. GregMeach

    GregMeach

    Joined:
    Dec 5, 2012
    Posts:
    249
    Change this:
    Code (CSharp):
    1. void OnGui()
    To this:
    Code (CSharp):
    1. void OnGUI()
    * Case MaTtERs ;-)
     
    Turbo420 and sbroscious like this.
  3. sbroscious

    sbroscious

    Joined:
    Jun 25, 2014
    Posts:
    4
    Thanks. I'm really feeling pretty dumb right now. I can't believe I didn't notice that. I guess I figured Unity would have thrown some type of exception for an erroneous function call as it warns me when I have so much as a variable that is unused.

    Do you have any idea why it allowed the code to run without any issue other than not doing what it was "supposed" to have done. Knowing this may help me solve future problems.

    Thanks again for helping me solve my problem ;)
     
  4. GregMeach

    GregMeach

    Joined:
    Dec 5, 2012
    Posts:
    249
    No problem, and I've noticed the root cause of the error too many times.

    Just as your OnGui() didn't run, nor does OntriggerEnter() and the miriad of other built-in Unity methods that ( from what I can recall) DO NOT autocomplete in MonoDevelop. I mean as soon as you type "void On" we should get a plethora of choices but none appear.

    Sorry I come from an XCode background and say what you want about Apple, they had autocomplete pretty well in-hand.
     
  5. Ghorre

    Ghorre

    Joined:
    May 7, 2014
    Posts:
    11
    What You code in Your scripts is classes.
    In a class definition you define Your own methods.

    It's that some of the methods are used as built-in and deriving/overriding (or however we should call it) those makes things happen in Unity.

    This + the case sensitivity = case solved :p

    So OnGUI() is treated as the built-in method and is called by Unity engine, while OnGui() is treated as a custom method and not used by any internals by default - You have to explicitly call it somewhere for it to work...
     
    sbroscious likes this.