- Home /
Can't draw GUI in Update() anymore?
So here is my Manager to make things all clean, :3 The point in this is to make everything GUI Related in one script... Just having a problem with the GUI...
You used to be able to draw GUI functions in Update() although now you can't, you must put them in OnGUI(), but the problem is... you can't call OnGUI() from another script, you can only call Update()... Which makes this very confusing for me.
This is the way I used to do it
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ApplicationManager : MonoBehaviour {
//Declaring HealthManager as an instance
public static ApplicationManager instance;
// getting the instance, if it doesn't exist create a new one.
public static ApplicationManager GetInstance()
{
if (instance == null)
{
instance = new ApplicationManager();
}
return instance;
}
public ApplicationManager() {
}
// Use this for initialization
void Start () {
instance = this; // Sets the instance to itself
}
// Update is called once per frame
void Update () {
HealthManager.GetInstance().Update();
ExperienceManager.GetInstance().Update();
StatManager.GetInstance().Update();
GUIManager.GetInstance().Update();
}
}
Although I get the error "GUI Functions can only be called from within OnGUI"
So I attempted to change GUIManager.GetInstace().OnGUI();
Which gave me the same error... O_O
Because it allows me to have all of my scripts managed and clean without worrying about how they are ordered, etc. When using the $$anonymous$$onoBehaviour you have to worry about in what order your components are in, etc. If I do it this way they are all bound together reguardless of order. It's one big thread. This is the way most servers handle their players, it's a timer that calls another timer. I remember back when I was doing Runescape Emulators ins$$anonymous$$d of Update() it was called Process()
It's just a clean way I like to do it, without attatching 500 scripts to anything and destroying my inspector. Also saves my CPU a LOT of stress.
I think I understand what you meant by that question, so I'll answer it again. You're probably thinking "But... Update is called automatically every frame" ... That's correct, when the script is attatched to something, however I only attach my Application$$anonymous$$anager script to a gameObject called "ScriptHandler" from there on it's all bound together via instancing, the reason I call Update manually is to get the functionallity out of the other scripts without applying them manually or adding them as an actual component.
I don't see what's the problem here. The OnGUI of your GUI$$anonymous$$anager gets called anyway, it can even be called multiple times per frame (if there are multiple pending events). If you think about this, it makes sense, because of the way GUI is implemented. I think you would be better off using Unity's proposed architecture for your scripting. If you really need to get ordering, you can either use LateUpdate or check this out:
http://docs.unity3d.com/Documentation/Components/class-ScriptExecution.html
You can put your classes in there and Unity will order their execution within frames.
Ding! Just had one of those idea things, just going to draw the GUI inside the Application$$anonymous$$anager script? shrug
Answer by drizztmainsword · Jan 21, 2013 at 04:40 PM
If you want to keep everything as part of ApplicationManager, and ApplicationManager is the only class that is inheriting from MonoBehavior, then try calling the GUI functions from ApplicationManager's OnGUI() function.
Thus, ApplicationManager would look like:
void Update () {
HealthManager.GetInstance().Update();
ExperienceManager.GetInstance().Update();
StatManager.GetInstance().Update();
}
void OnGUI() {
GUIManager.GetInstance().Update(); // Or GUIManager.GetInstance().OnGUI();
}
Didn't see this answer, but this is exactly what I started doing.
Answer by Bunny83 · Jan 21, 2013 at 04:37 PM
You really should never call Unity callback fucntions manually. Just use your own functions for this. Common names for Update-like functions are "Think", "Do", "Execute", "Process".
Your main problem however is that you can't use any GUI stuff in "Update". With Update i talk about the Unity callback, not your own functions. The restriction for GUI stuff comes from the fact that Unity has to prepare the engine for GUI drawing. When Unity is ready to draw the GUI it calles the OnGUI callback. Again it's important that Unity invokes OnGUI. When you call OnGUI from Update it won't work.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers
Unity GUI Button Options 0 Answers
GUI.PasswordField getting erased after clicking on the PasswordField. 0 Answers
'Back to Main Menu' button works, but cannot click menu buttons after returning! 1 Answer