First GUI Script Help(Solved)

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 :stuck_out_tongue:

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

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 functionOnGUI() ). 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:

function Start() {
}

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

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:

GUI.Box( Rect( 10,10, 100,10 ), "Test GUI" );
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:

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.

:slight_smile:

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:

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:

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

5 - Let’s pull it all together!

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!

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

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.

:frowning:

Now fixed. Enjoy!

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 :frowning:

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());
}

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

–Eric