Can someone explain to me how to make a simple selection system? What I have now is ray casting method that lets me select between two objects. But, the problem is that I can’t really select between the two correctly. For example: if I have Object A and Object B and I select Object A and than Object B I can’t select Object A again; same problem if i select in reverse order.
void OnMouseDown()
{
GameObject theGameManager = GameObject.Find("GameManager");
GameManager GameManagerScript = theGameManager.GetComponent<GameManager>();
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
// check if PlayerUnit 1 is selected
if (hit.collider.name == "PlayerUnit 1")
{
Debug.Log("PlayerUnit 1 selected");
GameManagerScript.is_PlayerUnit_1_Selected = true;
GameManagerScript.is_PlayerUnit_2_Selected = false;
GameManagerScript.PlayerUnit1.GetComponent<Unit>().enabled = true;
GameManagerScript.PlayerUnit1.GetComponent<UnitPath>().enabled = true;
GameManagerScript.PlayerUnit2.GetComponent<Unit>().enabled = false;
GameManagerScript.PlayerUnit2.GetComponent<UnitPath>().enabled = false;
return;
}
// check if PlayerUnit 2 is selected
else if (hit.collider.name == "PlayerUnit 2")
{
Debug.Log("PlayerUnit 2 selected");
GameManagerScript.is_PlayerUnit_1_Selected = false;
GameManagerScript.is_PlayerUnit_2_Selected = true;
GameManagerScript.PlayerUnit1.GetComponent<Unit>().enabled = false;
GameManagerScript.PlayerUnit1.GetComponent<UnitPath>().enabled = false;
GameManagerScript.PlayerUnit2.GetComponent<Unit>().enabled = true;
GameManagerScript.PlayerUnit2.GetComponent<UnitPath>().enabled = true;
return;
}
}
}