Boolean Toggle

Hello here i have a script that controlles my units, you can select and deselect them and then tell them what to do. I have a problem tho. When i select i also want to deselect a unit (when in collider). I can select but not deselect did i do something wrong in the script? The boolean i want to select and deselect (as seen in script) is “ImSelected”.

here is my script THANKS:

#pragma strict

private var agent : NavMeshAgent;
var Player : Transform;
var WallPosition : Transform;

var FollowPlayer : boolean = false;
var imSelected : boolean = false;
var InCollider : boolean = false;

function Start ()
{
	Player = GameObject.Find("Player").transform;
	WallPosition = GameObject.Find("WallPosition").transform;
	
	agent = gameObject.GetComponent(NavMeshAgent);
}

function Update ()
{
	//HotKeys
	if(Input.GetKeyDown(KeyCode.F2) && imSelected == true)
	{
		FollowPlayer = true;
	}
	
	if(Input.GetKeyDown(KeyCode.F1) && imSelected == true)
	{
		FollowPlayer = false;
		agent.SetDestination(WallPosition.position);
	}
	
	//Other
	GetComponent.<Animation>().Play("medievalSoliderMove");
	
	if(FollowPlayer == true)
	{
		agent.SetDestination(Player.position);
	}
	
	if(imSelected == false)
	{
		FollowPlayer = false;
	}
	
	if(imSelected == true && InCollider == true && Input.GetKeyDown(KeyCode.E))
	{
		imSelected = false;
	}
	
	if(imSelected == false && InCollider == true && Input.GetKeyDown(KeyCode.E))
	{
		imSelected = true;
	}
}

function OnTriggerStay (col : Collider)
{
	if(col.tag ==  "Player")
	{
		InCollider = true;
	}
}

function OnTriggerExit (col : Collider)
{
	if(col.tag ==  "Player")
	{
		InCollider = false;
	}
}

if(imSelected == true && InCollider == true && Input.GetKeyDown(KeyCode.E))
{
imSelected = false;
}

if(imSelected == false && InCollider == true && Input.GetKeyDown(KeyCode.E))
{
  imSelected = true;
}

This is the issue. In the first if(), you’re checking imSelected for true and setting it false. Then in the second if(), you’re checking for false and setting it to true. Instead you need to use an Else so it can only hit one or the other if():

if(imSelected == true && InCollider == true && Input.GetKeyDown(KeyCode.E))
{
  imSelected = false;
}
else if(imSelected == false && InCollider == true && Input.GetKeyDown(KeyCode.E))
{
  imSelected = true;
}