- Home /
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.
Answer by Casiell · Aug 22, 2018 at 09:50 PM
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
Answer by II_Spoon_II · Aug 22, 2018 at 10:14 PM
@Casiell Thank you very much!! worked :D 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?
When assigning TargetTower check if BaseTower is a different object. If no then null them.
if (isTarget)
{
//Assign BaseTower
}
else
{
if (this.gameObject != SelectionScript.selectedBaseTower)
//Assign TargetTower
else
{
SelectionScript.selectedBaseTower = null;
SelectionScript.selectedTargetTower = null; //this line is theoretically redundant
isTarget = false;
}
}
@Casiell Thank you again works like a charm, really appreciate it :)
Your answer
Follow this Question
Related Questions
position.Set vs. position = new Vector3() 0 Answers
GetMouseButtonDown(1) true in multiple frames 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Making an AirPlane follow mouse 1 Answer