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 MattDahEpic · Apr 25, 2014 at 03:18 AM · onguiguilayoutscreen.widthscreen.height

Only using related functions, yet still being told that I can't use non GUI functions in OnGUI (Don't call OnGUI)

I have a in game pause menu triggered when pressing escape, called using

if (Input.GetKeyDown(KeyCode.Escape)) { isPaused = true; } if (isPaused == true) { mouseLook.enabled = false; motionControl.enabled = false; OnGUI (); }

Which then calls OnGUI. The error, ArgumentException: You can only call GUI functions from inside OnGUI occurs within the OnGUI with the stacktrace? pointing in the bottom 2 lines to: on the second from bottom line, the GUILayout.BeginArea line within the OnGUI function; on the bottom most line, where the OnGUI is called in the code above.

I have a feeling it's the GUILayout.BeginArea line, which uses Screen.width and Screen.height. I think the error is referring to the use of the Screen class?, which I am using to center the GUI on the screen. Code for the entire OnGUI is as follows:

Code:

function OnGUI () { if (isPaused == true) { GUILayout.BeginArea(Rect((Screen.width / 2) - Screen.width / 4,(Screen.height / 2) - Screen.height / 4,Screen.width / 2,Screen.height / 2)); GUILayout.BeginVertical(); if (GUILayout.Button(returnMessage)) { mouseLook.enabled = true; motionControl.enabled = true; isPaused = false; } // This area is commented out because it doesn't work // if (Input.GetKeyDown(KeyCode.Escape)) { // mouseLook.enabled = true; // motionControl.enabled = true; // isPaused = false; // } if (GUILayout.Button(returnToMainMenuMessage)) { Application.LoadLevel(0); } if (GUILayout.Button(quitMessage)) { Application.Quit(); } GUILayout.EndVertical(); GUILayout.EndArea(); } }

Full Error Code (the copyable part):

ArgumentException: You can only call GUI functions from inside OnGUI. UnityEngine.GUIUtility.CheckOnGUI () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUIUtility.cs:228) UnityEngine.GUILayout.BeginArea (Rect screenRect, UnityEngine.GUIContent content, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUILayout.cs:281) UnityEngine.GUILayout.BeginArea (Rect screenRect) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUILayout.cs:270) InGamePauseMenu.OnGUI () (at Assets/Scripts/InGamePauseMenu.js:38) InGamePauseMenu.Update () (at Assets/Scripts/InGamePauseMenu.js:32)

All refenced code is located in the InGamePauseMenu script, testing for escape in in Update function, OnGUI is OnGUI. Line 38 in the GUILayout.BeginArea line and line 32 is where OnGUI (); is called in the first referenced section of code (update function).

===

TL;DR: I think the error occured because of using Screen.width and Screen.height in the rectangle define area of GUILayout.BeginArea

EDIT: You don't call OnGUI

Comment
Add comment
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

1 Reply

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

Answer by getyour411 · Apr 25, 2014 at 03:18 AM

I don't think you can call "OnGUI" in that way (ie top script line 8, remove that) and there's no need, OnGUI runs many many times a frame

Edit: glad that did it, changed to Answer.

Comment
Add comment · Show 1 · 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 MattDahEpic · Apr 25, 2014 at 03:24 AM 0
Share

that was quick! it works and that fixes the mistake I made in that other script. Thanks!

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make area adapt to screen size 1 Answer

Touchscreen input: Moving the player using screen.width & screen.height 1 Answer

Getting Device Screen Height and width in inches 2 Answers

How to resize a normal image then apply as a texture for GUI.Box()? 0 Answers

Screen.width & height not updated after screen rotation 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