Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Baalhug · Nov 22, 2013 at 03:33 PM · c#errorfunction

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I can do this in JS or not ??? 2 Answers

NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnCollisionEnter2D (UnityEngine.Collision2D coll) 1 Answer

Unity Internal Compiler Error, not there in Editor 0 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges