- Home /
Show the tooltip when mouse over at the object
Hi, i wanted to show the tooltip like this when the mouse over at the object, here is the example image:
i already tried this below code, but the message on the debug.log didn't showed up when i am hovering my mouse to the object, the object i give the name same like this:
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit) && hit.collider.gameObject.name == "Yify")
{
Debug.Log("Yify");
}
}
And here is my object (i use List to multiple the objects and each object i gave the name), (The object's name "Yify" is on the right side, dark green color):
Please help. Thank you.
Answer by markmmiller · Nov 27, 2013 at 06:35 PM
Tried the code that you've written and seems fine to me. Are you sure your tag is correct on the object within the scene?
Also depending on what you are doing I would recommend changing your code over to this format here if you are going to check multiple tags, this way you won't send out multiple rays for each check in a single frame.
if (Physics.Raycast(ray,out hit))
{
if(hit.collider.gameObject.name == "staticObject")
{
// Display tool tip
Debug.Log("staticObject");
}
}
I would also not recommend using just the tags to decide what is displayed. You could instead attach a class to the object (TooltipInfo) and have a check to see if that class is attached to that object, then have a general DisplayTooltip() function that will display all associated information related to that Tooltip object.
yes sir, i looked at it and it is the correct object, but when i hover the mouse to that object, nothing happen and the debug log also not come out
It's difficult to say without seeing the rest of your project. I can confirm that the should work. Have you checked the case of the tag? Have you ensured the script is attached to something within the scene?
If you could try the following code to ensure the ray is being cast correctly. It will also tell you the name and tag of whatever objects it collides with.
if (Physics.Raycast(ray,out hit))
{
Debug.Log("Hit detected with, Name : " + hit.collider.gameObject.name + ", Tag : " + hit.collider.gameObject.tag);
}
Yes sir, the raycast working properly, but the if statement after the raycast, is not executed! Here is the screenshot when i tried your last code (just a raycast, without hit.collider.gameObject.name), but when i hovering the mouse to the object, the Debug.Log still display "Tile(clone)"
Here is the screenshot when i tried "hit.collider.gameObject.name == Yify" (nothing happen when i hovering the mouse to the object):
and here is the code that i set the name of the object (red one object) to the "Yify":
void GenerateObjects()
{
UserPlayer player;
//AIPlayer aiplayer;
//First turn
player = ((GameObject)Instantiate(UserPlayerPrefab, new Vector3((mapSize - 2) - $$anonymous$$athf.Floor(mapSize / 2), 1.5f, -(mapSize - 1) + $$anonymous$$athf.Floor(mapSize / 2)), Quaternion.Euler(new Vector3()))).GetComponent<UserPlayer>();
player.gridPosition = new Vector2(mapSize - 2, mapSize - 1);
player.playerName = "Yify";
players.Add(player);
Thank you sir
Don't see where you set objects name. player.name = "Yify";
anywhere?
The name 'List' does not denote a valid type ('not found')?
These comes in Java Script
Please help me any one
Answer by belvita · Nov 29, 2013 at 04:02 PM
I think it is because u r using .name .it usually doesnt work properly so try this it will work
void Update() {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray,out hit)
{
if(hit.collider.gameObject.tag== "Your game object tag name")
{
Debug.Log("Yify");
}
}
}
Answer by Moor · Nov 30, 2013 at 04:45 AM
var TText:String="";
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 1000)) {
if(hit.collider.name=="Yify")
TText=hit.collider.name;
else TText="";
}
function OnGUI(){//for tooltip
if(TText!="")//GUIStyle to decorate the label
GUI.Label (Rect (Input.mousePosition.x, Screen.height-Input.mousePosition.y, 100, 20), TText);
}
better tag all the element, which u want for tooltip and refer check with tagg like
if(hit.collider.tag=="TTTag")//Line 6
Cannot sir, the TText always null, the program not executed on TText = hit.collider.name
use this
if(hit.collider.gameObject.tag== "Your game object tag name")
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Why is Unity crashing every time I press play in the editor after switching scenes? 0 Answers
invalid arguements for UIToolkit C# 1 Answer
Set the position of GUIBox 1 Answer