0
\$\begingroup\$

I want to display the image using a tooltip. So when I point to the 3dobject, it will display the image that I already created. Right now I already set the sprite in the tooltip but I have an error in the function HideToolTip. In this function, I want to hide the image when the arrow/cursor is away from the object.

------TooltipManager Script-----

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class TooltipManager : MonoBehaviour
{
    public static TooltipManager _instance;

    public Image imageComponent;

    //public SpriteRenderer imageComponent;
    // SpriteRenderer spriteRenderer = gameObject.GetComponent<SpriteRenderer>();

    private void Awake()
    {
        if (_instance != null && _instance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            _instance = this;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        Cursor.visible = true;
        gameObject.SetActive(false);
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = Input.mousePosition;
    }

    public void SetAndShowToolTip(Sprite Image)
    {
        gameObject.SetActive(true);
        imageComponent.sprite = Image;
    }

    public void HideToolTip()
    {
        gameObject.SetActive(false);
        imageComponent.sprite = Sprite.Destroy();
    }

}

Right now, I have an error in HideToolTip function.

------Tooltip Script-----

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Tooltip : MonoBehaviour
{
    public Sprite Image;

    private void OnMouseEnter()
    {
        TooltipManager._instance.SetAndShowToolTip(Image);
    }

    private void OnMouseExit()
    {
        TooltipManager._instance.HideToolTip();
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ A question mentioning an error should always include the full text of the error message. What led you to believe that imageComponent.sprite = Sprite.Destroy(); was a valid or useful thing to write? What do you intend that this line should accomplish? \$\endgroup\$
    – DMGregory
    Commented May 21, 2023 at 14:36
  • \$\begingroup\$ This is the error that I got --------------> Assets\Code\TooltipManager.cs(95,40): error CS1501: No overload for method 'Destroy' takes 0 arguments \$\endgroup\$ Commented May 24, 2023 at 10:27
  • \$\begingroup\$ Actually, I still find the proper way to hide the image by using Sprite. so I found to use sprite.Destroy but I still not know how to use it. But I know, if use text it will be -------> textComponent.text = string.Empty; So I try to change it from text to image based on the code in bold. \$\endgroup\$ Commented May 24, 2023 at 10:30

2 Answers 2

1
\$\begingroup\$

If you look up the documentation of the method Destroy, you will see that it requires the thing you want to destroy as an argument.

So instead of imageComponent.sprite = Sprite.Destroy();, you would write:

Destroy(imageComponent.sprite)

However, destroying the whole sprite asset just to hide the tooltip might be a bit radical. It would be better to use the Image.visible property:

imageComponent.visible = false;

Note how reading the documentation can literally save you days of waiting for people to answer your questions on the Internet. Being able to find and read documentation is one of the most important skills for a software developer.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for give me some tips. Maybe because I really don't like this code thing make me harder to read the documentation. thank you again. \$\endgroup\$ Commented May 25, 2023 at 13:43
0
\$\begingroup\$

You can use Unity event system and IPointerEnterHandler, IPointerExitHandler interfaces so something like this:

using UnityEngine;
using UnityEngine.EventSystems;

public class Tooltip : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    [SerializeField] private GameObject tooltipPanel;
    public void OnPointerEnter(PointerEventData eventData)
    {
        tooltipPanel.SetActive(true);
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        tooltipPanel.SetActive(false);
    }
}

Just make sure you have a physic raycaster on your camera and collider on objects. If you use it on UI make sure the UI objects has raycast target enable.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for the code and I already apply it in my project but I still have the erro at part Destroy. this is the error I got ------------------------------> Assets\Code\TooltipManager.cs(95,40): error CS1501: No overload for method 'Destroy' takes 0 arguments \$\endgroup\$ Commented May 24, 2023 at 10:26
  • \$\begingroup\$ The compiler is telling you what is the error method requires some arguments and you aren't giving any. What do you wish to achieve in that code from what I see you want to empty the sprite variable then just do imageComponent.sprite = null; . If you want to destroy that gameobject do Destroy(sprite) but I don't think you want it like that. \$\endgroup\$
    – Ivan
    Commented May 24, 2023 at 11:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .