# First GUI Script Help(Solved)

**URL:** https://discussions.unity.com/t/first-gui-script-help-solved/403009
**Category:** UI
**Tags:** IMGUI
**Created:** [December 3, 2009, 7:49pm UTC](https://discussions.unity.com/t/first-gui-script-help-solved/403009 "2009-12-03T19:49:13Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_89002125](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@anon\_89002125](https://discussions.unity.com/u/anon_89002125)
#### Post date: [December 3, 2009, 7:49pm UTC](https://discussions.unity.com/t/first-gui-script-help-solved/403009/1 "2009-12-03T19:49:13Z")

</div>

I must be over tired or just slow.

I can’t get the first basic script in the GUI Documentry to work.  
Do I do this in JAVA Script?  
Do I delete the entire Java script code and throw in the one they give, or do I paste it in the middle.

I understand Visual Basic for coding, though its not very helpful here 😛

If someone can point me to the right door I’ll be good lol

---

<div class="post-metadata">

### Author: ![AndrewGrayGames](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/andrewgraygames/32/561831_2.png) [@AndrewGrayGames](https://discussions.unity.com/u/AndrewGrayGames)
#### Post date: [December 3, 2009, 8:15pm UTC](https://discussions.unity.com/t/first-gui-script-help-solved/403009/2 "2009-12-03T20:15:44Z")

</div>

I’ve gotten a GUI to work out, so I can help ^^.

First, your GUI scripts are all event-based (as you can probably tell from function_On_GUI() ). So, let’s start this from scratch.

1 - Let’s create a new JavaScript - call it “testGUI.js” or something. It should start off looking something like this:

```auto
function Start() {
}

```

2 - Now, we need to make one simple modification: change Start to OnGUI, so the script will look like this:

```auto
function OnGUI() {
}

```

3 - Now, we’re going to want something to be shown ON our GUI. We have a few basic possibilities in the Unity GUI API: Box, Label, Button.

Box and Label are similar. They code something like this:

```auto
GUI.Box( Rect( 10,10, 100,10 ), "Test GUI" );

```

```auto
GUI.Label( Rect( 10,10, 100,10 ), "Test GUI" );

```

These are functions that live in Unity’s GUI library. Box creates a box with “Test GUI” as it’s title caption. See the Rect() call in it? This is simply telling the Box to draw in a rectangle from (10,10), that is 100 pixels wide and 10 pixels deep.

So, we can make an easy GUI:

```auto
function OnGUI() {
  GUI.Label( Rect( 10,10, 100,10 ), "Hello, world!" );
}

```

This code should make the phrase, “Hello, world!” appear in the top corner of the viewport when you run the game.

🙂

4 - To make things a bit more interactive, let’s add a quit button. (You have to build the application to see a quit button work, but here’s how you do it anyhow:)

We’re going to create a button. This is also a GUI call, but is slightly different from the Box and Label GUI objects:

```auto
if( GUI.Button( Rect( 10,10, 100,10 ), "This Game Sucks" ) ) { }

```

GUI.Button acts just like Label or Box - it has a rectangular region declared, and shows text, _however_ input to Buttons are detected in if() blocks. Now, we wanted to make a quit button? We just need to add one line of text:

```auto
if( GUI.Button( Rect( 10,10, 100,10 ), "Quit" ) ) { Application.Quit(); }

```

5 - Let’s pull it all together!

```auto
function OnGUI() {
  if( GUI.Button( Rect( 10,10, 100,10 ), "Quit" ) ) { Application.Quit(); }
}

```

Test run your game, or if you feel especially daring, build a development build and run it, then hit the quit button!

---

<div class="post-metadata">

### Author: ![anon\_89002125](https://avatars.discourse-cdn.com/v4/letter/a/85e7bf/32.png) [@anon\_89002125](https://discussions.unity.com/u/anon_89002125)
#### Post date: [December 4, 2009, 12:05am UTC](https://discussions.unity.com/t/first-gui-script-help-solved/403009/3 "2009-12-04T00:05:10Z")

</div>

Thanks, I got it working fine now.

Had to throw it with GUI Text, then turn off the pre-set GUI Text.

Now Its all making sense haha

Also Application.Exit Gave me the error, not .Quit

---

<div class="post-metadata">

### Author: ![AndrewGrayGames](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/andrewgraygames/32/561831_2.png) [@AndrewGrayGames](https://discussions.unity.com/u/AndrewGrayGames)
#### Post date: [December 4, 2009, 3:47am UTC](https://discussions.unity.com/t/first-gui-script-help-solved/403009/4 "2009-12-04T03:47:24Z")

</div>

Oh, good. I’ll just remove that false addendum.

I’m still working out how to include custom artwork. A few minutes with the tutorials would likely do much for me. That, or The Gimp, and an actual artist.

Glad I could help, and good luck on your game!

EDIT: I realized I left out a parenthesis on the Button section on every code block.

☹

Now fixed. Enjoy!

---

<div class="post-metadata">

### Author: ![warkarma](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/warkarma/32/341299_2.png) [@warkarma](https://discussions.unity.com/u/warkarma)
#### Post date: [December 9, 2009, 12:00am UTC](https://discussions.unity.com/t/first-gui-script-help-solved/403009/5 "2009-12-09T00:00:03Z")

</div>

Is it possible to show a number (int) in GUI ? Instead of text. Like for example:

var number:int =1;  
function OnGUI ()  
{  
GUI.Box (Rect (10, 10, 100, 50), number);  
}

I realy need to add number ☹

---

<div class="post-metadata">

### Author: ![Eric5h5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/eric5h5/32/44225_2.png) [@Eric5h5](https://discussions.unity.com/u/Eric5h5)
#### Post date: [December 9, 2009, 12:01am UTC](https://discussions.unity.com/t/first-gui-script-help-solved/403009/6 "2009-12-09T00:01:04Z")

</div>

Use ToString().

–Eric

---

<div class="post-metadata">

### Author: ![warkarma](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/warkarma/32/341299_2.png) [@warkarma](https://discussions.unity.com/u/warkarma)
#### Post date: [December 9, 2009, 12:06am UTC](https://discussions.unity.com/t/first-gui-script-help-solved/403009/7 "2009-12-09T00:06:27Z")

</div>

> [@""](#):
>
> Use ToString().
> 
> –Eric

example please (I am new to Unity)

(Found the answer)

var number : int = 1;

function OnGUI () {  
GUI.Box (Rect (10, 10, 100, 50), number.ToString());  
}

---

<div class="post-metadata">

### Author: ![Eric5h5](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/eric5h5/32/44225_2.png) [@Eric5h5](https://discussions.unity.com/u/Eric5h5)
#### Post date: [December 9, 2009, 12:14am UTC](https://discussions.unity.com/t/first-gui-script-help-solved/403009/8 "2009-12-09T00:14:02Z")

</div>

GUI.Box (Rect (10, 10, 100, 50), number.ToString());

–Eric
