Rect contains game object?

Hi, I want to check if a rectangle contains a gameobject position but its not detecting even if the gameobject is perfectly at the center of the rectangle, it works fine with Input.mousePosition . My code:

void Start () {
	aimRect = new Rect(Screen.width/2 - width/2,Screen.height/2-height/2,width,height);
}

// Update is called once per frame
void OnGUI () {

	if(aimRect.Contains(cubePos.position)){
		GUI.Box(aimRect,"Box In Rect");
	}else
		GUI.Box(aimRect,"This is a title");
}

Your rect is only working with Input.mousePosition because you place your rect at the center of the screen. You have three coordinates systems here: World, Screen, and GUI. Your object lives in world space, mousePosition lives in Screen space, and you are drawing the box in GUI space. So to fix the problem you can do:

void OnGUI () {
    Vector3 pos = Camera.main.WorldToScreenPoint(cubePos.position);
    pos.y = Screen.height - pos.y;

    if(aimRect.Contains(pos)) {
        GUI.Box(aimRect,"Box In Rect");
    }
    else {
        GUI.Box(aimRect,"This is a title");
    }
}