C# - Creating an Array of Vector 3s

I am trying to create 2 Vector 3 arrays, one to hold positions and one to hold rotations. The issue is on the first line of the start function. I get the error…

IndexOutOfRangeException: Array index is out of range.
CheckpointPos.Start () (at Assets/Scripts/CheckpointPos.cs:18)

I’ve looked around the forums to find answers but I can’t find anything that works =(
I also discovered if I try to edit the size of the array in the inspector, it automatically resets to 0. Any help appreciated and I’ll try answer any questions/clarify anything.

public Vector3[] SpawnPos = new Vector3[8];
public Vector3[] SpawnRot = new Vector3[8];
                 
        	 void Start(){
        		SpawnPos [0] = new Vector3 (-11.4f, 5.2f, 10.8f);
        		SpawnRot [0] = new Vector3 (335.8f, 0f, 0);
        		SpawnPos [1] = new Vector3 (-11.4f, 19.4f, 23.4f);
        		SpawnRot [1] = new Vector3 (305.9f, 0f, 0f);
        		SpawnPos [2] = new Vector3 (-11.4f, 25.4f, 40.2f);
        		SpawnRot [2] = new Vector3 (2.2f, 0f, 0f);
        		SpawnPos [3] = new Vector3 (-11.4f, 21.6f, 57.4f);
        		SpawnRot [3] = new Vector3 (32.7f, 0f, 0f);
        		SpawnPos [4] = new Vector3 (-11.4f, 5.3f, 75.9f);
        		SpawnRot [4] = new Vector3 (90f, 0f, 0f);
        		SpawnPos [5] = new Vector3 (-11.4f, -13.0f, 57.2f);
        		SpawnRot [5] = new Vector3 (51.3f, 185.5f, 184.8f);
        		SpawnPos [6] = new Vector3 (-11.4f, -13.0f, 57.2f);
        		SpawnRot [6] = new Vector3 (51.3f, 185.5f, 184.8f);
        		SpawnPos [7] = new Vector3 (-11.4f, -11.1f, 12.3f);
        		SpawnRot [7] = new Vector3 (51.3f, 0f, 0f);
            }

The initialization in code of an a public variable is only done at the time the script is attached. After that, the only thing that matters are changes you make in the Inspector. So if you change the size of the array (or just added initialization code for the arrays) after the script was attached, chose changes will not be reflected in the public variables. So you need to take a look at the size of these arrays in the Inspector. You can also use ‘Reset’ from the gear drop down on the right side of your component in the Inspector to reread the initialization code.

If it works with the rest of your code, a simpler solution would be to change both arrays to be private.

I know this appears solved but a bit of more clarification doesnt hurts anyone.

If you try to restart the scene it will lose all your variables initialization if it is not used this way:

    //Variable declaration:
    
    public Vector3[] SpawnPos; 
    public Vector3[] SpawnRot;
    private int spawnTotal;
    
    Start(){
    spawnTotal=8; // dynamic
    SpawnPos = new Vector3[spawnTotal];
    SpawnRot = new Vector3[spawnTotal];
    
    SpawnPos [0] = new Vector3 (-11.4f, 5.2f, 10.8f);
                    SpawnRot [0] = new Vector3 (335.8f, 0f, 0);
                    SpawnPos [1] = new Vector3 (-11.4f, 19.4f, 23.4f);
                    SpawnRot [1] = new Vector3 (305.9f, 0f, 0f);
                    SpawnPos [2] = new Vector3 (-11.4f, 25.4f, 40.2f);
                    SpawnRot [2] = new Vector3 (2.2f, 0f, 0f);
                    SpawnPos [3] = new Vector3 (-11.4f, 21.6f, 57.4f);
                    SpawnRot [3] = new Vector3 (32.7f, 0f, 0f);
                    SpawnPos [4] = new Vector3 (-11.4f, 5.3f, 75.9f);
                    SpawnRot [4] = new Vector3 (90f, 0f, 0f);
                    SpawnPos [5] = new Vector3 (-11.4f, -13.0f, 57.2f);
                    SpawnRot [5] = new Vector3 (51.3f, 185.5f, 184.8f);
                    SpawnPos [6] = new Vector3 (-11.4f, -13.0f, 57.2f);
                    SpawnRot [6] = new Vector3 (51.3f, 185.5f, 184.8f);
                    SpawnPos [7] = new Vector3 (-11.4f, -11.1f, 12.3f);
                    SpawnRot [7] = new Vector3 (51.3f, 0f, 0f);
}

`

About the correct use of methods and variable spelling for better coding:

CAPS in the first letter are for Methods for better coding. So, SpawnPos spelled this way seems like a method to anyone familiar with this. Please use spawnPos for variables. SpawnPos if it is a method. Its hard at first but do it.