0
\$\begingroup\$

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);

            }
        }


    }
```
\$\endgroup\$
6
  • 2
    \$\begingroup\$ We'll need more context of your code to be able to help you with this - what does your declaration of the SelectedUnits variable look like? How does your second script get its reference to the UnitController object 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\$
    – DMGregory
    Nov 19 '19 at 0:09
  • \$\begingroup\$ It looks like this 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\$
    – Kiyo
    Nov 22 '19 at 16:36
  • \$\begingroup\$ Why did you make this a static variable, then try to access it like it's an instance variable? \$\endgroup\$
    – DMGregory
    Nov 22 '19 at 17:01
  • \$\begingroup\$ I figured that I would only want one set of selected units and the contents of which should be the same. At least thats what I learnt of static variables. \$\endgroup\$
    – Kiyo
    Nov 22 '19 at 19:02
  • \$\begingroup\$ it says it needs an object reference otherwise \$\endgroup\$
    – Kiyo
    Nov 22 '19 at 19:04
0
\$\begingroup\$
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
            {


                foreach(var Unit in UnitController.SelectedUnits)
                {
                    Debug.Log("Running loop");
                    Unit.transform.Find("SC").GetComponent<MeshRenderer>().enabled = false;
                }
                UnitController.SelectedUnits.Remove(gameObject);

            }
        }
    }

I just had to define where the foreach loop had to find the variable.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.