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 Ochreous · Jun 10, 2013 at 01:03 AM · c#guitooltip

C# GUI.Tooltip If Statement

Hey everyone, is there a way to put an if statement inside of a GUI.Tooltip? I want an if statement to check if a variable is null and if it is don't display it in the GUI.Tooltip. I'm not sure if I want to put the if statement inside of the GUI.Button or above the GUI.Label.

 using UnityEngine;
 using System.Collections;
  
 public class example : MonoBehaviour {
 public int someInt;
 public string someString;
 public float someFloat;
 void OnGUI() {
 GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Click me", someInt.ToString(),"\n"+someString,"\n"+someFloat.ToString()));
 GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip);
    }
 }
Comment
Add comment · Show 1
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 Scribe · Jun 10, 2013 at 10:33 AM 0
Share

which value do you want to check if null, and if null which parts of the tooltip do you not want to display.

Also I'm pretty sure Tooltip doesn't take 3 strings as an argument so you will probably get errors from that too.

Scribe

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Leslie-Young · Jun 10, 2013 at 12:04 PM

 public int someInt;
 public string someString;
 public float someFloat;
 void OnGUI() 
 {
     GUI.Button(new Rect(10, 10, 100, 20), new GUIContent(
         "Click me",
         someInt.ToString() + 
         (string.IsNullOrEmpty(someString) ? "" : "\n"+someString) +
         "\n"+someFloat.ToString()));
     GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip);
 }
Comment
Add comment · Show 1 · 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 Scribe · Jun 10, 2013 at 12:27 PM 0
Share

I don't see enough people making use of the ? operator, this is what I would do aswell.

+1

Scribe

avatar image
0

Answer by ByteSheep · Jun 10, 2013 at 01:22 AM

You can just create the tool tip string as a variable like this:

 void OnGUI() {
 
   if(someVar == null)
   {
     // someVar is null so don't add it to the tool tip
     var buttonString = someFloat.ToString();
   }
   else
   {
     // someVar is not null so add it to the tool tip
     var buttonString = someFloat.ToString()+"\n"+someVar.ToString();
   }

   GUI.Button(new Rect(10, 10, 100, 20), new GUIContent("Click me", buttonString));
   GUI.Label(new Rect(10, 40, 100, 40), GUI.tooltip);
 }
Comment
Add comment · Show 2 · 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 Ochreous · Jun 10, 2013 at 03:23 AM 0
Share

Where would you declare someVar?

avatar image InfiniBuzz · Jun 10, 2013 at 12:15 PM 0
Share

someVar would be the variable you want to check. You said you want to check if a variable is null so I assume you already have this variable? Or do you want to set a flag(bool) and check if its true or not?

If you want to check if the strings you want to pass are 'existing' check Leslie Young's answer

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

C# Multiple GUI.Tooltips 2 Answers

What is frame, How OnGuI is called every frame? 2 Answers

C# GUI Destroy or Disable? 1 Answer

FPS keep a loadout 0 Answers

How to increase space between two things with GUILayout.BeginHorizontal C# 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