Adding Rectangles to a List

As part of my GUI, I define rectangles for each character’s GUI elements, such as inventory windows etc. The position and dimension for these rectangles are the same between different characters.

My main game controller has a list where these rectangles are added if a character is viewing the inventory, and removed if not. Then I iterate through the list’s contents to see if the mouse is inside any of the rectangles (to disable raycasting etc).

Now comes the problem:

The list does not store the rectangles as unique items but rather just looks at the rect’s attributes (position and dimension), so rects from different characters get overwritten or otherwise confused.

How could I manage this?

Character GUI script: (for, let’s say, two or more characters)

showInventory : boolean;
invWindowZone  = new Rect(100,100, 200, 200);

function Update() {
if(showInventory == true) {
		if(actorControl.guiZones.Contains(invWindowZone) == false) {
			GameControl.guiZones.Add(invWindowZone);
		}
	}
	else {
		if(actorControl.guiZones.Contains(invWindowZone) == true) {
			GameControl.guiZones.Remove(invWindowZone);
		}
	}
}

GameControl GUI script (unique)

var cursorIsInMainView : boolean;
var guiZones : List.<Rect>;
function Update() {
var cursorIsInMainViewInt : int = 0;
	for(var rectCur : Rect in guiZones) {
		if (rectCur.Contains(Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y), true)) {
			cursorIsInMainViewInt = 1;
		}
	}
	
	if(cursorIsInMainViewInt == 1) {
		cursorIsInMainView = false;
	}
	else {
		cursorIsInMainView = true;
	}
}

So I found a solution which is a little bit more processor-intensive but it works.

I used the method described in DaveA’s answer in this thread:

As he says, I check the mouse position against each rectangle in each character script’s OnGUI() function, and set the mouseIsOverGUI boolean to true if yes. In the main game controller script, in LateUpdate(), I set this variable to false.