How to access lists that have a dynamic count?

Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class rayManager : MonoBehaviour {

    [Tooltip("Diameter.")]
    [Range(0.125f, 5)]
    public float holeSize;
    public int resolution;
    public GameObject model;

    [Range(5f, 15f)]
    public float modelDistance;
    public Vector3 modelRotation = new Vector3();
    public Vector3 modelSize = new Vector2();

    private float height;
    private float width;
    private List<Vector3> spawnLocation = new List<Vector3>();
    private Vector3 cubeScale = new Vector3();
    private Vector3 cubePosition = new Vector3();
    private List<Vector3> point = new List<Vector3>();
    public GameObject cubeTop;
    public GameObject cubeBottom;

  
    void Awake()
    {
      

        model = Instantiate(model, new Vector3(-modelDistance, 0f), Quaternion.Euler(modelRotation));

        model.transform.localScale = modelSize;

        if (resolution <= 2)
            resolution = 2;
        if (holeSize <= 0.125)
            holeSize = 0.125f;

        cubeTop = Instantiate(cubeTop);
        cubeBottom = Instantiate(cubeBottom);

        CalculateSpawnPoints();
    }

    void Update()
    {


    
        Transform modelTransform = model.transform;
        modelTransform.localPosition = new Vector3(-modelDistance, 0f);
        modelTransform.localRotation = Quaternion.Euler(modelRotation);
        modelTransform.localScale = modelSize;



        height = Camera.main.orthographicSize * 2;
        width = height * Screen.width / Screen.height;

        cubeScale = new Vector3(width * 0.005f, (height - holeSize) / 2, 1);
        cubePosition = new Vector3(0, height / 4 + holeSize / 4);

        cubeTop.transform.localScale = cubeScale;
        cubeTop.transform.localPosition = cubePosition;

        cubeBottom.transform.localScale = cubeScale;
        cubeBottom.transform.localPosition = -cubePosition;

        cubeBottom.transform.name = "Bottom Cube";
        cubeTop.transform.name = "Top Cube";

        if (resolution <= 2)
            resolution = 2;

      

    }

    public void CalculateSpawnPoints()
    {

        spawnLocation.Clear();
           
            float spawn = 0f;

            for (spawn = -holeSize; spawn <= holeSize; spawn += holeSize * 2 / resolution)
            {
                spawnLocation.Add(new Vector3(0f, spawn));
                Debug.Log(spawn);
            }

        Debug.Log(spawnLocation[0]);
        Debug.Log(spawnLocation[resolution]);
          
    }

}

Nothing here is really as important as the list, but I gave the whole script in case it helps. So the script will have its Count as big as resolution+1. I don’t want to access every Vector3 in the list manualy, but instead I want to make it so that somehow I can make the code access it itself. The vectors are just spawn points. If you want to you can put the script in a project and just fill everything out and it should work. Also if I was hard to understand or something is unclear please ask in the comments its late here so I might make no sense.

You can access the Nth element of a list with myList[N], which it looks like you are already doing.

You can see how many elements are in the list with myList.Count. For example, you can iterate over all items in the list like this:

for (int index = 0; index < myList.Count; ++index)
{
    var currentItem = myList[index];
    // Do something with currentItem
}

(You could also iterate over the list using a foreach loop, but the for version is more illustrative of other things you can do with lists.)

A common thing to do with a list of spawn locations is to select one at random. A typical way to do that would be something like:

int randomIndex = Random.Range(0, myList.Count);
var randomItem = myList[randomIndex];
1 Like

Ah thanks I thought I should use a for loops but couldnt see how. Thanks for the idea!