- Home /
OnGui not working (again)
Hi, me again, I can't make a Gui work, here's my script, I've tried with both "OnGUI" and "OnGui" and I can't get it work. Am I doing something wrong??!!
I started a new project, and attached my c# script to my camera, it didn't work, I tried with empty objects and nothing, I also tried with Button, Text , and also Nothing!!
public class Example : MonoBehaviour {
void OnGui ()
{
GUI.Label (new Rect (10, 10, 100, 20), "Hello World!");
}
}
public class Example : MonoBehaviour {
void OnGUI ()
{
GUI.Label (new Rect (10, 10, 100, 20), "Hello World!");
}
}
Answer by jokim · Aug 21, 2014 at 04:07 PM
First off, the right syntax is the second one : OnGUI
Other than that, everything seems ok... now, not to sound rude or anything, but here's a few things that might prevent it from working :
Is your script file named "Example.cs" ?
Did you save the script ?
Did you hit play ?
I actually copied the code and ran, it works fine.
yep, yep and yep, I have worked with scripts but never with OnGUI, which is new for me
Another thing i forgot to mention, but it's probably already there, seeing as you can use GUI...
Did you include the right namespaces ?
using UnityEngine;
using System.Collections;
And, When you say "it doesn't work"
What exactly happens ? Do you have errors in the console ? or it runs without showing texts ?
Try adding this in the OnGUI method :
Debug.Log("Test");
and see if the console shows something
Hi, yes I include:
using UnityEngine; using System.Collections;
and I don't have errors, runs normally but doesn't show the text
it's weird because I started a new project, did the same thing and I still can't see the text.
Indeed, this is weird, I started a new project, made a single script, called Example, copied your code... and it's working on my side...
I even reverted layouts to factory settings, i didn't change my $$anonymous$$ain camera, it's the one that comes from a new Scene... back drop is blue, text is white...
I have no idea what's going wrong on your side... Back-up your projects and Reinstall ?
I was almost sure it wasn't something I was doing, I reinstalled Unity and it worked again, I don't know why or how, but now it work. thanks for your help.
Answer by armada29 · Apr 24, 2016 at 07:35 AM
hi. is it too late? i had same problem and figured out it's because of lighting problem. first off, check the window -> lighting setting. then just the skybox set to None. if you may see the gui, then solve just lighting problem.
Answer by Jason-King · Oct 15, 2016 at 12:34 AM
Another reason that the OnGUI could appear to not be working is if you changed the Game's Free Aspect Scale to a large enough value that the text is simply off the camera view. Yes, it can be that simple.
But that can't be the case here since a Debug.Log statement inside OnGUI should fire at least two times each frame. Since it doesn't there's a different problem. The most common ones would be:
The script isn't attached to any gameobject in the scene.
The script is disabled in the inspector
The gameobject or any parent gameobject is deactivated in the inspector.
5 years later and your comment helped me get past this issue after starting over twice (neglected to attach script to the gameobject).
Answer by mini-lucy · Jun 26, 2017 at 03:14 AM
Would you try to tap the "2D" button in the Scene? I'm the beginner of Unity coding but It seems it matters that it is 3d or 2d.
Your answer
![](https://koobas.hobune.stream/wayback/20220613161159im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUI script giving weird error 1 Answer
EditorGUILayout.Foldout not working properly - results in argument exceptions 1 Answer
OnGUI() not showing anything 2 Answers
OnGUI button changes size for resolution, but text doesnt? 1 Answer
How to make a GUI window appear on clicking a game object 0 Answers