Unity Right Click On Object

Viewed 170

I am new to unity. Currently, I am working on a small project.

I am creating a solar system where the player can click on planets and the main camera will zoom in and follow that planet.

The player can also right-click on a particular planet and the name of that planet will be displayed. However, if the player re-clicked the right click again, the name will disappear (need help with that)

I used OnMouseOver with Input.GetMouseButton (see below). But I am stuck and have no idea how to stop displaying the text when the player re-click right-click.

Can anyone please help me :)

What I have used:

private void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(1))
        {
            text.SetActive(true);
        }
        
    }
2 Answers

You can use 1 variable to storage the current state and switch on/off state.

bool active = false;
private void OnMouseOver()
    {
        if (Input.GetMouseButtonDown(1))
        {
            active = !active;
            text.SetActive(active);
        }
        
    }

Using an additional field like in this answer isn't even necessary.

You can simply do

private void OnMouseOver()
{
    if (Input.GetMouseButtonDown(1))
    {
        text.SetActive(!text.activeSelf);
    }
}

See

Related