• 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 flatline115 · Dec 12, 2013 at 09:36 PM · assign-variable

How to make a Label a variable

I have been attempting to make a GUI Label a variable - but it always errors.. Am I doing anything wrong?

 using UnityEngine;
 using System.Collections;
 
 public class clue : MonoBehaviour {
 
     void Start () {
         Screen.SetResolution(1024, 768, true);
     }
     
     void Update () {
 
     }
     void OnGUI () {
         // Main Menu.
         var height =  Screen.height;
         var width  =  Screen.width;
         var label =  GUI.Label (new Rect (width/2, height/2, 100, 200), "Menu:");
 
 
     }
 }
 
Comment
Add comment · Show 5
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 Josh707 · Dec 12, 2013 at 09:48 PM 0
Share

I don't think you can use it as a variable, it doesn't return anything and it's not a class itself or variable type. What are you trying to do with it?

avatar image flatline115 · Dec 12, 2013 at 09:49 PM 0
Share

I will be wanting to get rid of it after about 5 seconds so I thought I would assign it as a variable.

avatar image Josh707 · Dec 12, 2013 at 09:53 PM 0
Share

Oh, I think you'll have to set it up with a timer/coroutine to turn it on and off, I don't know any other ways of doing it.

avatar image flatline115 · Dec 12, 2013 at 10:02 PM 0
Share

Thanks, for the information.

avatar image kiritaku · Dec 12, 2013 at 10:48 PM 0
Share

GUI.Label is a call you make inside of OnGUI(). its not persistant, it just causes the label to be drawn that one time. if the next time OnGUI() is called, you don't call GUI.Label, then your label will disappear.

1 Reply

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

Answer by Saad_Khawaja · Dec 12, 2013 at 11:18 PM

If you wish to turn it off after 5 seconds, the easiest way would be to use a boolean flag and turn it off.

You can use a coroutine or Invoke to set the flag off after some seconds.

void Start () {

     Invoke("turnLabelOff",5f);
 }
 
 bool flag = true;
 void OnGUI()
 {
     if(flag)
     {
         GUI.Label(new Rect(0f,0f,100f,20f),"Off in 5 seconds");
     }
 }
 
 void turnLabelOff()
 {
     flag = false;
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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 avatar image

Related Questions

Error assigning to a variable 0 Answers

How to assign Image gameobject to a variable via script? C# 2 Answers

Can I attach the Main Camera to two scripts? 1 Answer

How to assign a private transform of my player in script? 1 Answer

GUI.DrawTexture and assign it to a variable? 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