So I finally perfected my RTS multiple movement. The situation is that the game objects are added into a list in a script bound to the camera called UnitController they are added from a unit client script MultiselectClient, this client accesses the UnitController class and adds itself the gameObject into the list. Each time I do the drag select I want it to clear the list first before adding any more. But for some reason, it doesn't work?
This is in my UnitController (below):
void MultiSelect(RaycastHit hit)
{
Debug.Log(SelectedUnits);
if (SelectedUnits.Count > 0)
{
Debug.Log("Moving group to: " + hit.point);
foreach(GameObject gameObject in SelectedUnits)
{
gameObject.GetComponent<NavMeshAgent>().SetDestination(hit.point);
}
}
}
This is in my client (below):
public bool Selected = false;
void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position);
camPos.y = Screen.height - camPos.y;
//if inside the cameras drag selection area then mark player as selected
Selected = UnitController.selection.Contains(camPos);
Debug.Log("Selected");
if(selected){
addTo();
}
}
}
void addTo()
{
UnitController.SelectedUnits.Remove(gameObject); //I have also tried .clear() which also hasnt worked.
if (!UnitController.SelectedUnits.Contains(gameObject))
{
UnitController.SelectedUnits.Add(gameObject);
Debug.Log("Added to list: " + UnitController.SelectedUnits);
}
else
{
Debug.Log("Im already in that list.");
}
}