Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LPGaming-Company · Jan 21, 2013 at 03:09 PM · gui

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

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kat0r · Jan 21, 2013 at 04:14 PM 0
Share

Why would you even call Update() manually?

avatar image LPGaming-Company · Jan 21, 2013 at 04:16 PM 0
Share

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.

avatar image LPGaming-Company · Jan 21, 2013 at 04:18 PM 0
Share

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.

avatar image $$anonymous$$ · Jan 21, 2013 at 04:30 PM 2
Share

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.

avatar image LPGaming-Company · Jan 21, 2013 at 04:36 PM 1
Share

Ding! Just had one of those idea things, just going to draw the GUI inside the Application$$anonymous$$anager script? shrug

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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();
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jan 21, 2013 at 04:50 PM 0
Share

Exactly ;)

avatar image LPGaming-Company · Jan 21, 2013 at 05:34 PM 0
Share

Didn't see this answer, but this is exactly what I started doing.

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges