I’ve been working on an inventory script and for some reason when I use a for loop to create multiple buttons it will cause multiple tooltips to display when hovering over all the buttons except the last one. So if I use a for loop to create 3 buttons in total and then hover over the first button all 3 tooltips display for each button, but if I hover over the third button then just it’s tooltip displays. Is there a piece of code I can add to my for loop to make the button only display it’s own tooltip?
The first step would be to post your code here so we could look it up and say what’s making it wrong…
The only answer so far I can give to you is, that you should check your buttons for multiple tooltip creation. You’re just allowed to show one tooltip, and have to replace it’s info, so it might be a good idea to use a string variable as tooltip for the buttons.
But without the code it’s just a guessing game
Guess you’re right lol.
for(var x = 0; x < MainInventory.Count; x++) {
if(GUI.Button(Rect(Screen.width/2, Screen.height/2 + ( 20 * x), 100, 20), GUIContent ( MainInventory[x].Name,
"Stamina: " + MainInventory[x].Stamina + "\n" +
"Power: " + MainInventory[x].Power + "\n" +
"Attack Speed: " + MainInventory[x].attackSpeed + "\n" +
"Rarity: " + MainInventory[x].rarity ))) {
}
GUI.Label(Rect(Screen.width/2 + 150, Screen.height/2 + ( 20 * x), 300, 300), GUI.tooltip);
}
You create a Label with each iteration. you should place the GUI.Label “after” the for-loop. You only need “one” tooltip Label to be shown, it will be replaced by the currently used tooltip. Maybe that should fix the multiple tooltip problem (as hint: place the tooltip as last GUI action, then it will always be on top of everything
Would look like this:
for(var x = 0; x < MainInventory.Count; x++)
{
if(GUI.Button(Rect(Screen.width/2, Screen.height/2 + ( 20 * x), 100, 20), GUIContent ( MainInventory[x].Name,
"Stamina: " + MainInventory[x].Stamina + "\n" +
"Power: " + MainInventory[x].Power + "\n" +
"Attack Speed: " + MainInventory[x].attackSpeed + "\n" +
"Rarity: " + MainInventory[x].rarity )))
{
}
}
GUI.Label(Rect(Screen.width/2 + 150, Screen.height/2 + ( 20 * x), 300, 300), GUI.tooltip);