So I have a method that checks to see if you clicked on an ‘enemy’ gameobject. I want to be able to deselect the enemy by clicking away, say on some terrain, and I tried doing this with booleans.
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
foreach (string enemy in enemyTitles)
{
if (Physics.Raycast(ray, out hit) && target != null)
{
target.transform.position = hit.point;
//Check to see if you right-clicked on an enemy from the array.
if (hit.transform.gameObject.tag == enemy)
{
enemySelected = true;
Debug.Log("engaging now." + enemy + "" + enemySelected);
//Set global variable to the selected enemy's tag.
selectedEnemy = enemy;
chosenEnemy = hit.transform.gameObject;
target.transform.position = chosenEnemy.transform.position;
}
else if (hit.transform.gameObject.tag != enemy){enemySelected = false;}
}
}
}
The problem is, enemySelected is true only for a single frame, as the next frame overwrites it, I want it to be true UNTIL I click away. I was wondering what the best way to handle this would be?