Hello unity comunity i have a problem with my scripts, ican’t seem to find a way to deselect. for now i can select one unit and move but can’t unselect and so each time i select a unit it will move with the others.
here my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class selectunits : MonoBehaviour
{
public bool selected = false;
public GameObject Player;
// Use this for initialization
void Start()
{
gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;
gameObject.GetComponent<movement>().enabled = false;
}
// Update is called once per frame
void Update()
{
}
private void OnMouseDown()
{
if (Input.GetMouseButtonDown(0))
{
if (GameObject.FindWithTag("US")){
selected = true;
if (selected == true)
{
Player.GetComponent<movement>().enabled = true;
gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = true;
}
else
{
if (Input.GetMouseButtonDown(0))
selected = false;
if (selected == false)
{
Player.GetComponent<movement>().enabled = false;
gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;
}
}
}
}
}
}
`
Note: i have a simple movement script a part(it’s a simpe unity navmesh movement).
In line 26 you set the variable selected to true; then immediately afterwards, you have an if-condition checking if selected is true. Of course it is - you just said so. So the block after the else in line 32 can never ever be executed. Maybe that’s causing your problem? Sorry I can’t give you a more definitive answer because I cannot deduce from context, what exactly your code does (e.g. why you are checking for a game object with Tag “US” in line 25).
You probably want to change this line 26 to
selected = !selected;
which “flips” selected, i.e. if it was true before, it is false now and vice versa. But still then, the check for MouseButton 0 in line 35 doesn’t make sense, because you already checked that and the whole code block opened in line 24 will only be called if MouseButton 0 is down. Similarly, the check if selected is false in line 37. It must be, because otherwise you wouldn’t be in the else-block opened in line 34.
Some other (unrelated, performance-focused) advice:
AFAIK GetComponent() is quite expensive. Instead of doing it in OnMouseClick every time, I guess it would be better performance-wise to get the references for the movement-component and the NavMeshAgent just once on Awake() and store those in private variables.
If you don’t do anything in Update(), you should delete it from the script I think.
I take it, that this script is attached to an object you want to select.
The problem is with this part of the code:
if (GameObject.FindWithTag("US")){
// Once you are here, you make the flag for 'selected' to true.
selected = true;
// So, when you reach this place, 'selected' is already true
if (selected == true)
{
Player.GetComponent<movement>().enabled = true;
gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = true;
}
// Basically, you can never reach this else statement.
else
{
if (Input.GetMouseButtonDown(0))
selected = false;
if (selected == false)
{
Player.GetComponent<movement>().enabled = false;
gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;
}
}
}
What you should do instead, is to switch selected to the opposite state:
if (GameObject.FindWithTag("US")){
// Instead of directly setting this value, set it to the opposide of what it already is
selected = !selected;
// And now you can shorten your code significantly as well.
// If 'selected' was 'false' before, after pressing the button it's now 'true' so the code below would execute
if (selected == true)
{
Player.GetComponent<movement>().enabled = true;
gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = true;
}
// If on the other hand 'selected' was 'true' before, after pressing the button it's now 'false' so the code below would execute
else
{
Player.GetComponent<movement>().enabled = false;
gameObject.GetComponent<UnityEngine.AI.NavMeshAgent>().enabled = false;
}
}