I think my C# is a bit lacking here but I am trying to access a component in a list of objects in another class. I am trying to access UnitController from MultiSelectClient and I'm not sure how to do this. I can remove the objects from the list but I can't seem the access each one even if I try a ForEach loop, I can't wrap my head around it.
UnitController.SelectedUnits //Is the list I'm trying to access.
Ive tried doing
UnitController.SelectedUnits.Find/FindAll(gameObject)//but that returns an error
This is where the list is defined
public class UnitController: MonoBehaviour
{
bool clicked = false;
bool dragging = false;
//public float speed = 10f;
//public float rotationSpeed = 10f;
public LayerMask groundLayer;
public NavMeshAgent playerAgent;
public static Vector3 mouseDownPoint;
public static Vector3 mouseUpPoint;
public static Vector3 currentMousePoint;
public static Rect selection = new Rect(0, 0, 0, 0);
private Vector3 startClick = -Vector3.one;
public Texture2D selectionHighLight;
public static List<GameObject> SelectedUnits;
public RaycastHit firstRay;
public RaycastHit secondRay;
private void Start()
{
SelectedUnits = new List<GameObject>();
}
This is the script that needs to access it. At least I want to be able to iterate through the list and access each unit in the list and its components.
public class MultiSelectClient : MonoBehaviour
{
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();
}
else
{
UnitController.SelectedUnits.//something
UnitController.SelectedUnits.Remove(gameObject);
}
}
}
```
SelectedUnitsvariable look like? How does your second script get its reference to theUnitControllerobject it wants to work with, if it's a non-static variable? See past Q&A on topics like this for some leads that might help. \$\endgroup\$public static List<GameObject> SelectedUnits;I just want to be able to use the find function in the other script to access one of the units in the list and access something in that unit. \$\endgroup\$