- Home /
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.
I am inheriting $$anonymous$$onobehaviour, still it is not working. Please help
Answer by whydoidoit · Jul 13, 2012 at 11:03 AM
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.
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.
Answer by jetty112 · Oct 19, 2012 at 06:14 PM
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?
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 $$anonymous$$onoBehaviour. If you want to write GUi code in another class you must call that code from a $$anonymous$$onoBehaviour.
Your answer
Follow this Question
Related Questions
OnGUI Button 1 Answer
Making a text box appear after a certain gameObject is destroyed 0 Answers
Filling image without compromises 0 Answers
GUILayout.Label is not displaying on level load 1 Answer
Script is not working help plz 0 Answers