Selecting objects for a level editor

Hi,

I am working on a level editor for a 2d game were you can create objects and position them and change there shape and stuff.

I have most of it worked out and sorted but one thing I am stuck on is a system for the player to select an object that they want to change. I mean I know how to click on it with a raycast but my theory was when you click on an object you set the tag to “selected” so the Gui knows what to change, but then how would I deselect something when a player selects something else? If the player clicked on another object it would simply change that objects tag to “selected” and leave the other one as is.

What would be ideal would be a system that checks all the movable game objects in the game if they have the “selected” tag then changing them to “deselected”.

is this possible? If so how would I go about doing it?

thanks dearly for all the help in advance!

You probably want a ‘selection mangager’ class that does this. A singleton or plugin script. At some point, casting a ray will hit something else you can select, or hit nothing. In that case, it changes any ‘selected’ to ‘unselected’ and selects accordingly.

You might consider using a selection property instead of tagging items as selected. This might look like

javascript

var selection : GameObject;

function Update () {
	// If left mouse is pressed react accordingly
	if (Input.GetMouseButtonDown(0)) {
		// cast a ray from the mouse position as far as the camera can see
		var hit : RaycastHit;
		var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		var distance : float = Camera.main.farClipPlane;
		if(Physics.Raycast(ray, hit, distance)) {
			// if we hit anything then make it the new selection
			selection = hit.collider.gameObject;
		}
	}
	
	// So we can see what is selected while testing this script
	// let's make the selection spin
	if (selection) {
		selection.transform.RotateAround(selection.transform.position,
										 Vector3.up,
										 15.0 * Time.deltaTime);
	}
}