How to use UICamera.hoveredObject properly.

This is probably pretty simple and straight forward, I’m simply wanting to create an if statement based off of the information gathered from this syntax. So i created a simple cube, put it in the GUI layer…and the object’s name is “Cubey”.

function Update () 
{
	if (UICamera.hoveredObject.name == ("Cubey"))
	{
		print ("hit cubey!");
	}
}

It works but I get a null exception error???

NullReferenceException: Object reference not set to an instance of an object
testButtonCooldown.Update () (at Assets/Scripts/testButtonCooldown.js:10)

Line 10 is the if statement of the code I just posted.

If it isn’t ‘hovering’ any object, the hoveredObject variable will be null:

if (UICamera.hoveredObject != null && UICamera.hoveredObject.name == ("Cubey"))
    print ("hit cubey!");