Simple Loop thru array not working

Hi,

This is super simple, playing with a simple bowling game, 10 pins.
I’m trying to store starting position of the pins into Vector3 array called pinLocs[ ], and getting this warning:

Here is the code:

public class BowlingGame : MonoBehaviour {

    public GameObject[] pins;  //Pin GameObjects set in inspector
    private Vector3[] pinLocs ;

    // Use this for initialization
    void Start () {

        //Store all of the pins original locations for resetting, in pinLocsArray
        for(int i = 0; i < 10; i++) {

            pinLocs[i] = pins[i].transform.position ;

        }
           
    }

}

I’ve been staring at this for hours and cannot find what is wrong with the loop, no vector3 data is being assigned to pinLocs.

If I don’t do it in a loop, and assign the Vector3 data array item by array item, it says index out of range. This is ridiculously easy but I have no idea what is wrong. I’m sure once I publish this post and see an answer I will bonk myself in the head.

Of course I solve it 10 seconds after I post. The 2nd array has to be public for the script to work.

    public GameObject[] pins;  //Pin GameObjects set in inspector
    public Vector3[] pinLocs ;

Now that begs another question, why does the code require the Vector3 pinLocs array to be public in the inspector?

The issue isn’t public vs private, you never initialized the array.

pinLocks = new Vector3[pins.Length];
// Now you can use the pinLocks array

It just so happens that Unity takes care of that when it is declared public.

1 Like

Thank you for the reply, and not insulting me for my ignorance…

Just to be clear on why you need that line. A Vector3 is an entire struct with a lot of data contained in it. When you make a Vector3 the compiler sets a lot of space for that Vector3. You need some way to find out where that space is.
So when you type

Vector3 myVariable = new Vector3(0,0,0);

myVariable is a small block of memory, that just holds a pointer to a big block of memory that has all your Vector3 data.

So Vector3 myVariable means make a small block of memory that will point to a vector3
new Vector3() = make a big block of memory that holds all the actual Vector3 stuff.

So in your case you just told the compiler to make a small block of memory pointing to null(nothing) and you will fill it in later. Since you never used the new Vector that @DanielQuick shows.

You might ask why would you ever just type:

Vector3 myVariable;

without the new keyword. Well maybe another function is going to send you back a Vector3 and so you don’t need to waste time/memory making a Vector3 yourself that your just going to throw away. Like this:

Vector3 myVariable;

void Start()
{
        myVariable = GetMeAVector3();
}

private Vector3  GetMeAVector3()
{
        Vector3 someVector = new Vector(0,0,0);
        // Maybe some maths here to change that Vector3
        return someVector;
}

Now the method GetMeAVector3 is making that big block of memory that holds the Vector3 and passing you back the address of it to stick in your variable myVariable. (so you didn’t need to use the new keyword in Start). But somebody somewhere has to use the new keyword to actual make that Vector3.

Finally… Why did making it public work? you never used the new keyword?
When you make something public in Unity, that lets Unity know to show it in the inspector. For the Inspector to work there has to actual be something there. So when you type in :

public Vector3 myVariable;

Unity is making the Vector3 for you behind the scenes. Somewhere in the code you can’t see is the line myVariable = new Vector3(); That way the inspector has something to work with.