Hello, I was unsure what to title this haha.
I was trying to make a strategy game-like selection system using lists, every time you select the unit, a highlight object appears under the unit, and every time you click the ground, the highlight is supposed to disappear. The problem is whenever I add a list clear method, everything goes crazy. So here’s my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BimbusMove : MonoBehaviour
{
public Camera gameCamera;
Ray camRay;
RaycastHit hit;
List<Transform> selectedBimbi = new List<Transform>();
public void MoveBimbus(Vector3 dest)
{
}
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(camRay, out hit))
{
if (hit.transform.CompareTag("Bimbi"))
{
SelectBimbus(hit.transform);
}
else if (hit.transform.CompareTag("Terrain"))
{
DeselectBimbus();
}
}
}
}
private void SelectBimbus(Transform unit)
{
selectedBimbi.Clear();
selectedBimbi.Add(unit);
unit.Find("Highlight").gameObject.SetActive(true);
}
private void DeselectBimbus()
{
for(int i = 0; i < selectedBimbi.Count; i++)
{
selectedBimbi[i].Find("Highlight").gameObject.SetActive(false);
}
}
}
Don’t mind my goofy names XD
It works just fine, until I add the selectedBimbi.Clear() line. I want it to clear the previous click when I click the next one, but it just adds up ever unit. So, I added the clear method but all that does is make it so the previously selected unit is cleared, not all of them. Before I added the method, it clears all them properly. What am I doing wrong here?