How to make a gameObject disappear when a mouse hovers and reappear when the mouse leaves?

Greetings Unity Answers, this is my first time asking you.

The situation is:

  • There is a cube (gameObject).
  • If a mouse hovers over it, the cube disappears.
  • If the mouse leaves, the cube reappears.

This is a script I attach to the parent of the gameObject I want to make appear/disappear:

public class Visibility2 : MonoBehaviour 
    {
    	void OnMouseEnter ()
    	{
    		foreach (Transform child in this.transform)
    		{
    			child.gameObject.SetActive (false);
    		}
    	}
    
    	void OnMouseExit ()
    	{
    		foreach (Transform child in this.transform)
    		{
    			child.gameObject.SetActive (true);
    		}		
    	}
    }

I eventually got the basics right.

i.e Able to use setActive function using keyboard input or mouse button input and attach it to a parent so that (true/false) functions properly.

But the purpose of my question is to find out how to apply an OnMouseEnter/Exit-like input on a setActive function.

There is also a similar script that is also plausible:

public class Visibility : MonoBehaviour 
{
	void Update () 
	{
		foreach (Transform child in this.transform)
		{
			if (Input.GetMouseButtonDown(0)) 
			{
				child.gameObject.SetActive(false);
			}
			else if (Input.GetMouseButtonUp(0)) 
			{
				child.gameObject.SetActive(true);  
			}
		}
	}
}

Except that I want to hover with a mouse ,not press a button on a mouse, but as far as I know, there are no substitute Input functions that provides hovering (e.g like OnMouseEnter/OnMouseExit), but maybe this is my best idea I can only get.

It’s possible that I miss something or I am doing it wrong, but I am at a dead-end here so if you are able a, if not a full answer, at least a suggestion, that would be great, thanks.

I’'d like to confirm that I was able to solve the problem myself already, and that I shouldn’t jump to conclusions.

The Visibility2 script actually works, and the issue I ran to is that I hadn’t apply a collider to the parent, which is absolutely necessary in order for OnMouseEnter/Exit to work.

Thanks for the suggestions.