New to Unity, still learning C#, can someone explain arrays to me?

I’ve been reading a few books, already had a few months of programming experience but I’ve never “really” understood arrays, if someone could explain what they for and how to use them effectively with an example if possible then i’d appreciate it :smile: (currently studying C# 4+ hours per day so I might just be a bit overwhelmed with all the new info)

What kind of explanation are you looking for? All an array is really is an easier way to manage multiple objects.

For example:

GameObject Go1 = new GameObject();
GameObject Go2 = new GameObject();
GameObject Go3 = new GameObject();

vs.

GameObject[] gameObjects = new GameObject[3]
{
     new GameObject(),
     new GameObject(),
     new GameObject()
};

You’ll want to use them for managing or interacting with groups of objects, such as inventory items. For usage examples google for loops. Also, if you want a more advanced description you can always use
Microsoft’s Documentation http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
Unity community’s Explanation http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

An array is a way of storing many variables with the same name.

For example if you wanted to store the ages of 20 people you could use:
Age1 =20
Age2 =24
Age3 =56

All the way upto Age20, as you can see this is a hassle and a waste of time.

You can use an array with 20 elements in it instead
Age [20] this would define an array called Age with 20 elements.

Then to store the peoples ages you would use
Age[0] =20
Age[1] =24
Age[2] =56
upto
Age[19] = 34

well using the array it would consume just as many lines and you still would have to write the ages which would be as much of an hassle as doing what you said first, that’s what is causing me so much confusion >.< definitely missing something here

Not really because you can use this to fill the array

int[ ] Age = new int[20] {20, 24, 56,…,34};

And this example can fill an array with all game objects of tag ‘enemy’

function Start () {
        var gos : GameObject[];
        gos = GameObject.FindGameObjectsWithTag("enemy"); 
}

What you’re missing is if you wanted to add up all the ages and get a total, it would be a pain with the individual variables, but quite easy with the array.

In addition, if you then wanted to change the number of people in the list, it’d be much easier in the array version.

And that’s all assuming that you even know how many people there are going to be. If you don’t know, then you have no choice but to use an array.

Keep learning and programming. As you practice, you’ll find more and more reasons to do things the ‘right’ way.

Nobody has mentioned one of the real advantages to arrays, which is that you can iterate over them. So say you wanted to find the sum of everyones age:

int sumAge = 0;
foreach(int a in Age)
{
  sumAge += a;
}

And that you don’t have to know before hand how many different values. If you use an array as a public variable in Unity, you can fill it in with however many values you want via the inspector.

EDIT: Actually someone did mention it right as I was posting.

Imagine you want to add the value 10 to to 100 variables, would you write 100 lines to add the value, or would you use a for loop on an array like this?

for(int i = 0; i < 100; i++)
{
  nameOfArray[i] = 10;
}

Really simple example but there’s much more use to it than just that. Btw you can still do this if you like:

nameOfArray[0] = 10;
nameOfArray[1] = 10;
nameOfArray[2] = 10;
etc…

yea I figured that would work better but still its very complicated in my head, probably the first thing that is making me sweat from reading this all over again so many times and still not understand what else I can do with it. (maybe I’m just overlooking too much, I’m planning on learning C# to make a game and trying to think of a situation where I would use an array seems to be what is causing me not to “understand” it)

Another example.

I had a lot of booleans, i needed to check them one by one to see if they were true or false, and then do something accordingly. Also the number of booleans could increase, or decrease, so when I have 100 booleans, i have to write 100 if/else statements which clutters up the code, and isn’t dynamic at all, if I add a boolean, I have to add another if/else statement, which is impossible in runtime. If I remove a boolean, I have to remove the if/else statement, which also isn’t very flexible while the application is running.

So without an array, this could be a scenario, remember I have to write this for every boolean I want to check

if(boolean1 == true)
{
  print("bool1 is true");
}
else
{
  print("bool1 is false");
}

With an array I could do it like this, and even if I add/remove booleans from the array, the code will run as it should.

for(int i = 0; i < boolArray.Length; i++)
{
  if(boolArray[i] == true)
  {
     print("boolArray" + i.ToString() "  is true");
  }
  else
  {
     print("boolArray" + i.ToString() "  is false");
  }
}

I highly recommend to read up on for loops if you haven’t worked with them yet, arrays and for loops are like a very happy marriage :). Don’t get intimidated about for loops/arrays, they are very easy to work with once you understand the concept.

yea I understand loops perfectly since I’ve been using them for a long time I just haven’t used arrays that much which made me not understand them that well, thank you all for the help :smile: