how to check if mouse position is hovering on a gui rect

i want to use the Rect.contains() method to detect if my mouse is hovering over a gui rect.

i tried using this.

Update() {

   if (GameGUI.avatarArea.Contains (Event.current.mousePosition)) {

       Debug.Log("test");
     }

}

but i get this error “Object reference not set to an instance of an object”.

avatarArea is a Rect.
GameGUi is a class i made that handles GUI.

any help is appreciated.

Your code here won’t compile, so I’m not sure if this is pseudo-code or not. You must handle GUI things inside OnGUI(). I’m guessing that Event.current is null outside of OnGUI() which is why you are getting the error. If you must do the processing in Update(), then you should use Input.mousePosition and convert it to a GUI position (or have the rect in screen space rather than GUI space). To convert Input.mousePosition to a GUI position:

var guiPos = Input.mousePosition;
guiPos.y = Screen.height - guiPos.y;

But the best solution is to do the processing in OnGUI().