x


GUI functions inside OnGUI giving argument exception

I'm using the following code:

using UnityEngine;
using System.Collections;

public class TestScript{

 private GUISkin custom_skin;
 public bool guiEnabled;

 public TestScript(GUISkin new_skin){
 custom_skin = new_skin;
 }

 public void OnGUI(){
 if(guiEnabled){
 GUI.skin = custom_skin;
 GUI.Label(new Rect(0,0,50,50),"Blah");
 }
 }
}

The problem is that I'm also getting the following error: ArgumentException: You can only call GUI functions from inside OnGUI.

It works fine in another script that is running in the same scene, but for some reason this one won't. I looked at the other script, and it's all set up in exactly the same way, but for some reason I'm getting this error all of a sudden.

more ▼

asked Jul 13 '12 at 11:01 AM

kvdleij gravatar image

kvdleij
5 2 2 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Your TestScript doesn't inherit from MonoBehaviour so I presume you are calling its OnGUI yourself somewhere - it needs to be called from within a real OnGUI for it to work.

more ▼

answered Jul 13 '12 at 11:03 AM

whydoidoit gravatar image

whydoidoit
33k 11 23 99

Exactly ;)

Jul 13 '12 at 11:41 AM Bunny83

Yeah, apparently that was the problem. I was calling the OnGUI from within the Update from another script, and a colleague pointed out to me that that was the problem.

Jul 13 '12 at 01:05 PM kvdleij
(comments are locked)
10|3000 characters needed characters left

I am trying to use a similar thing, but the OnGUI() in the class wont work for the class objects. I want to call a render function in another script that then renders the class object, but the script never runs the OnGUI() function of that class object. What do?

more ▼

answered Oct 19 '12 at 06:14 PM

jetty112 gravatar image

jetty112
16 1 2 4

Please don't ask additional questions as an answer to a previous question. Use a comment or ask a new question and post a link to the related item.

OnGUI is only called in a MonoBehaviour. If you want to write GUi code in another class you must call that code from a MonoBehaviour.

Oct 19 '12 at 07:41 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3678
x242
x32

asked: Jul 13 '12 at 11:01 AM

Seen: 981 times

Last Updated: Oct 19 '12 at 07:41 PM