- Home /
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);
}
}
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
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);
}
I don't see enough people making use of the ? operator, this is what I would do aswell.
+1
Scribe
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);
}
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
Follow this Question
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