Make two successif selection with mouse?

I want to select a tower, than another, in an order “Base Tower” then “Target Tower”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Towers : MonoBehaviour {


    bool isTarget;      
    // Use this for initialization
    void Start()
    {
        isTarget = false;
    }

    // Update is called once per frame
    void OnMouseDown()
    {
            GameObject theGameManager = GameObject.Find("GameManager");
            TheGameManager SelectionScript = theGameManager.GetComponent<TheGameManager>();
       
            //if no tower is selected, the selected one will be the base tower
            if (isTarget == false)
            {
                SelectionScript.selectedBaseTower = this.gameObject;
                isTarget = true;
            }
            //if a tower is already selected, the second selection will be the target tower
            else if (isTarget == true)
            {
                SelectionScript.selectedTargetTower = this.gameObject;
                isTarget = false;
            }
           
        
    }
    void CancelMove() {
        GameObject theGameManager = GameObject.Find("GameManager");
        TheGameManager SelectionScript = theGameManager.GetComponent<TheGameManager>();
        //If the player wants to cancel his move after selecting the first tower, he clicks it again
        if (SelectionScript.selectedTargetTower || SelectionScript.selectedBaseTower)
            {
                SelectionScript.Move = true;
            }
        else if (SelectionScript.selectedBaseTower == SelectionScript.selectedTargetTower)
            {
                SelectionScript.selectedTargetTower = null;
                SelectionScript.selectedBaseTower = null;
                SelectionScript.Move = false;
            }
    }
}

The variables “selectedBaseTower” and “SelectedTargetTower” are in the Game Manager script I try to access them from the tower script, it works fine the problem is when I first select a tower (by clicking) and it’s get affected to the “selectedBaseTower” which is what I want, but after when I do the second click, the second tower dosen’t affect the TargetTower but the same BaseTower get replaced, I think my mistake is in teh bool implementation, but can’t figure it out.

isTarget should be either static or located in SelectionScript (preferably second option). Right now each tower has its own bool “isTarget” that is independent from every other tower.

Other remarks:

  • else if (isTarget == true) - the if part is redundant. When executing this statement isTarget will always be true
  • you don’t reset isTarget in CancelMove method

@Casiell Thank you very much!! worked :smiley:
I have one last problem, when selecting the same tower for target and base, I want the TargetTower and BaseTower become null, deselect everything, how can I achieve that?