- Home /
Im trying to create a tooltip when hovering over my gameobject
Hello everyone,
currently im trying to solve a problem I encountered, but I cant find anything online. I want to have a tooltip show when I hover over a planet that shows some variables about that planet. I am unsure how to do this, since Texts and Images work differently than regular gameobjects. Im trying to set a Text type active while hovering, as visible in my planetscript:
{ //UI and sprite public Sprite[] spritelist; public SpriteRenderer rend; public float makebigger = 1.3f; public bool madeBigger = false;
//Planet stats
public int fuelCost;
public int Coins;
public int Enemies;
public int FightStrength;
public int CrewCost;
public GameObject beginstation;
public Text statText;
void Start()
{
//Random sprite from list
rend.sprite = spritelist[Random.Range(0, spritelist.Length)];
fuelCost = Random.Range(1, 5);
Coins = Random.Range(1, 5);
Enemies = Random.Range(1, 5);
FightStrength = Random.Range(1, 5);
CrewCost = Random.Range(1, 5);
beginstation = GameObject.Find("BeginStation");
//Tooltip
statText.text = "Fuelcost: " + fuelCost + "Coins: " + Coins;
}
void OnMouseOver()
{
if (madeBigger == false)
{
transform.localScale *= makebigger;
madeBigger = true;
statText.gameObject.SetActive(true);
}
}
void OnMouseExit()
{
transform.localScale /= makebigger;
madeBigger = false;
statText.gameObject.SetActive(false);
}
void OnMouseDown()
{
Debug.Log("Enemies: " + Enemies);
Debug.Log("Coins: " + Coins);
Debug.Log("Fuel cost: " + fuelCost);
Debug.Log("FightStrength: " + FightStrength);
beginstation.GetComponent<WorldGenScript>().needSpawn = true;
}
I figured that this would show a certain text showing the variables for that specific planet, but it does not even turn off the text on the canvas. So how do I show a text when hovering over a gameobject, which shows some of the variables of that specific gameobject?
Thank you
What is your script sitting on?
And I couldn't clearly understand the problem.
Can you be more specific?
When you hover: does it activate the text object?
When you exit hover: does it deactivate?
Your code seems rather fine..
I managed to solve it by making a tooltip gameobject that has an image child and text child. Thanks for the time!
Great. That's where I was going to with my questions.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Retrieve Variable's [Tooltip()] Property From a Custom Inspector (C#) 2 Answers
C# Multiple GUI.Tooltips 2 Answers
C# GUI.Tooltip If Statement 2 Answers