# Getting and error

**URL:** <https://discussions.unity.com/t/getting-and-error/402536>\
**Category:** UI\
**Tags:** IMGUI\
**Created:** [November 26, 2009, 10:19pm UTC](https://discussions.unity.com/t/getting-and-error/402536 "2009-11-26T22:19:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon\_77443035](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@anon\_77443035](https://discussions.unity.com/u/anon_77443035)\
**Post date:** [November 26, 2009, 10:19pm UTC](https://discussions.unity.com/t/getting-and-error/402536/1 "2009-11-26T22:19:39Z")

</div>

Im new to programming in general, and just recently started with GUI, so here is my problem:

```auto
var GUIon : boolean = false;
var xPos : int;
var yPos : int;
var Height : int;
var Width : int;
var bText : String = "Text in here!";

function Update() {
	if ((Input.GetKeyDown(KeyCode.Escape)) GUIon == false) {
		GUIon = true;
		OnGUI();
	}
	
	if ((Input.GetKeyDown(KeyCode.Escape)) GUIon == true) {
		GUIon = false;
	}
}		

function OnGUI() {
	if (GUIon == true) {
		if (GUI.Button (new Rect(xPos,yPos,Height,Width), bText)) {
			print ("Button Pressed");
		}
	}
}

```

I get an error that sais “You can only GUI functions from inside OnGUI.”

how do i get rid of it?

---

<div class="post-metadata">

**Author:** ![anon\_2074824](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@anon\_2074824](https://discussions.unity.com/u/anon_2074824)\
**Post date:** [November 27, 2009, 12:59am UTC](https://discussions.unity.com/t/getting-and-error/402536/2 "2009-11-27T00:59:17Z")

</div>

You shouldnt call OnGUI() in the Update() method. In fact, in normal circumstances you shouldnt call OnGUI() at all, only override it. Unity will call it for you.

---

<div class="post-metadata">

**Author:** ![anon\_77443035](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@anon\_77443035](https://discussions.unity.com/u/anon_77443035)\
**Post date:** [November 27, 2009, 2:54pm UTC](https://discussions.unity.com/t/getting-and-error/402536/3 "2009-11-27T14:54:17Z")

</div>

ok thanks for the help

but when i take away the OnGUI(); the script doesnt work, the GUI dont show when i press Escape

---

<div class="post-metadata">

**Author:** ![anon\_2074824](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@anon\_2074824](https://discussions.unity.com/u/anon_2074824)\
**Post date:** [November 27, 2009, 3:27pm UTC](https://discussions.unity.com/t/getting-and-error/402536/4 "2009-11-27T15:27:10Z")

</div>

Thats because both IF statements in your Update() method will execute in the same frame when Escape is pressed, so GUIon gets immediately set to false right after it was set to true.

---

<div class="post-metadata">

**Author:** ![anon\_77443035](https://avatars.discourse-cdn.com/v4/letter/a/58f4c7/32.png) [@anon\_77443035](https://discussions.unity.com/u/anon_77443035)\
**Post date:** [November 27, 2009, 3:51pm UTC](https://discussions.unity.com/t/getting-and-error/402536/5 "2009-11-27T15:51:58Z")

</div>

ok, i changed the script so that first there is an IF then ELSE IF.

But it still doesnt work

EDIT: nvm got it working. just misspelled one thing.
