- Home /
OnGUI is not being called
I was attempting to create a main menu, and after inputting a 3D Options Menu, OnGUI refused to be called, I suspected this when my fade-in code stopped working, however the exact same code in another level worked perfectly, it fades in and fades out. To test if OnGUI was actually being called I added a small Label to test it. It was not renderered.
function OnGUI(){
//Debugging OnGUI (Because evidence inclines it's not being called)
GUILayout.BeginArea(Rect(10,10,200,20));
GUILayout.Label("OnGUI is being called");
GUILayout.EndArea();
if(firstLoad){
fadeTime = 2;
alphaFadeValue -= Mathf.Clamp01(Time.deltaTime / fadeTime);
GUI.color = Color(0,0,0,alphaFadeValue);
GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),fadeTexture);
if(alphaFadeValue <= 0){
firstLoad = false;
fadeTime = 0;
alphaFadeValue = 0;
}
}
if(isLoading){
fadeTime = 2;
alphaFadeValue += Mathf.Clamp01(Time.deltaTime / fadeTime);
GUI.color = Color(0,0,0,alphaFadeValue);
GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height),fadeTexture);
if(alphaFadeValue >= 1){
fadeTime = 0;
alphaFadeValue = 1;
Application.LoadLevel("Level1");
}
}
}
As you can see, the very first bit of code should be working, but it does not show up. It was working fine up until now.
Code i'm using for attempting to start a new game;
var CameraObject : Transform;
function OnMouseDown() {
var MenuOptions = CameraObject.GetComponent(MainCamera);
MenuOptions.isLoading = true;
}
I know that it's working because the 'isLoading' boolean is turned to true, as I can see in the Inspector, however the OnGUI function is not being called.
why do you assign isLoading as true, looks like you need to initialize firstLoad variable
firstLoad is called when the document is first loaded, so basically when the game is first loaded, it does a fade in, isLoading is for when the 'Start Game' button is clicked, because I wanted the GUI to be handled here in one script I created a Boolean that could be accessed by other scripts.
Have you done a simple Debug.Log inside of OnGUI just to be sure some other scipt isnt setting that boolean to false right away.
function OnGUI()
{
Debug.Log("OnGUI is being called");
}
Another thought i had, is this inside of a custom class? If it is in a class that does not extend $$anonymous$$onoBehaviour OnGUI will never be called.
Answer by prototype7 · Aug 30, 2012 at 09:51 AM
Try start a new Game with
var isLoading : false;
function OnMouseDown() {
if(!isLoading){
// ...load your Menu
}else{
// ...unload your Menu
}
isLoading = !isLoading;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613080352im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
C# - How do I increase the variable from another script? 2 Answers
RPC call failed 1 Answer
calling functions help {javascript} 1 Answer
Game responds slow to device event 0 Answers
Call a function once from OnGUI 2 Answers