- Home /
How can i call a GUI function?
I have 2 scripts: car.cs and GUIcarrera.cs. I use car.cs to define the behaviour of the car and GUIcarrera.cs to print time elapsed in the race and events that happen to my car in game, like running out of fuel. car.cs is asigned to every car object in game and GUIcarrera.cs is asigned to the camera.
I have tried different ways but none works and i dont know why. My code:
// Script GUI_carrera
using UnityEngine;
using System.Collections;
public class GUI_carrera : MonoBehaviour {
static GUIStyle customLabel;
public static void eventos(string texto){
GUI.Label(new Rect(Screen.width-250, 60, 240, 110), texto, customLabel);
}
}
From the script car i call the GUI function with:
GUI_carrera.eventos(txt_evento);
I just want a message appears in screen when the car goes out of fuel: txt_evento="You're out of fuel", but right now it passes the compilation but i got a runtime error: NullReferenceException: Object reference not set to an instance of an object
¿Any ideas?
Answer by Owen-Reynolds · Nov 22, 2013 at 03:47 PM
Maybe two things wrong:
o The GUI-dot functions have to be run from OnGUI. The main set of examples (http://docs.unity3d.com/Documentation/Components/gui-Basics.html) hints at this, but doesn't actually say it. Of course, they can be in some other function, called from OnGUI.
o GUI-labels are not "fire&forget," unlike setting colors. You can't set GUI.Label 1-time and have it stick to the screen. It has to be called every frame. As soon as you stop calling it, the labels stops being shown. A common way to do it is:
public showFuelWarning=false;
OnGUI() {
if(showFuelWarning) GUI.Label(...
}
To pop up the message, set showFuelWarning
to true. Then later can look up how to time things in Unity, to have it only stay for a few seconds.
Answer by aeau2012 · Nov 22, 2013 at 04:25 PM
Hey Baalhug, to call a GUI function, you just need to define the function: void OnGUI() in your class, inside of that function you can call all the GUI methods that you need
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnGUI.html
But for what you want is better to create a GUIText in the editor under Gameobject/createOther/GUIText
In the editor, edit it's properties put in text You're out of fuel.
HINT: the GUIText that you create directly in the editor, you change position for x between 0 and 1, and the same for y, starting that the position (0,0) of the GUI is the corner left up of the screen.
And Deactive it.
do a reference of this Text in your car.cs:
public GUIText Fuel;
Add the GUIText in the editor and when you need it, just call this reference Fuel.enabled = true;
Hope this help you!
Answer by Baalhug · Nov 22, 2013 at 06:26 PM
Thank you both. I've tried with standard OnGUI but it will run even if no events have happened so i wanted a part that always run, timer of race, which is actually in OnGUI and other functions to be called from different scripts.
Anyway, i've tried before GuiText but i can't get them visible in runtime, probably i was doing something wrong and looking for solutions i found GUI scripting.
Well, i'll try GUIText again, i hope i can find a solution.
TY, guys
Your answer
Follow this Question
Related Questions
I can do this in JS or not ??? 2 Answers
Unity Internal Compiler Error, not there in Editor 0 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer