Show the tooltip when mouse over at the object (88104)

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.

Does your object have a collider?

3 Answers

3

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); }

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

player.playerName = "Yify"; <-- here sir @yatagarasu

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");

         }
  }

}

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")