Help creating a basic array (C#)

Hey there Unity,

I haven’t had much use for arrays, but this is one I figure it would be incredibly more efficient to do so. Currently I have no idea where to start other than to make a gameObject array. I could really use a point in the right direction.

Also, if someone could explain the different between a list and array that would be helpful.

I’m not asking for a rewrite/fix or anything, but if you’re feeling generous I tend to learn from example. Just comment it out somewhat so I know what’s happening.

Else, I would appreciate a link where I can find out how to make arrays like this, and what parameters I need to use to do so.

Thanks!

By the way, this code is for a GUI window with changing text, two buttons (forward and backwards) and one exit button in the top right.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class StartingGUI : MonoBehaviour {
	//Audio 
	public AudioClip click;
	//Bools
	public bool showText1 = false;
	public bool showText2 = false;
	public bool showText3 = false;
	//GameObjects
	public GameObject Text1;
	public GameObject Text2;
	public GameObject Text3;

	void Start(){
		//Make our first window show
		showText1 = true;
	}
	void Update(){
		//If our first window bool is true
		if (showText1) {
						Text1.SetActive (true);
		//Make the gameObject holding the text active.
				} else {
		//If the bool isn't true..
			Text1.SetActive(false);
		//Set as inactive, disabling the gameObject.
				}
		if (showText2) {
			Text2.SetActive(true);
				} else {
			Text2.SetActive(false);
				}
		if (showText3) {
			Text3.SetActive(true);
		} else {
			Text3.SetActive(false);
		}
	}
	public void GoToRight (){
		//If we click the arrow-to-the-right button..
		transform.parent.audio.PlayOneShot (click,0.2f);
		//if we're on the first page..
		if (showText1) {
		//Go to the second page and disable the first one.
			showText2 = true;
			showText1 = false;
				}
		if (showText2) {
			showText3 = true;
			showText2 = false;
				}
	}
	public void GoToLeft (){
		transform.parent.audio.PlayOneShot (click,0.2f);
		if (showText2) {
			showText2 = false;
			showText1 = true;
				}
	}
	public void Exit (bool showGUI){
		//If we click the exit button, exit the window.
		transform.parent.audio.PlayOneShot (click,0.2f);
		transform.parent.GetComponent<Canvas> ().enabled = false;
	}

}

Everything you could possibly want to know, with examples :smiley:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

This article does a good job explaining

Lists are arrays that can be resized easily.

You declare an array like this

GameObject[] gameObjectArray;

You will need to initialise it with a length

gameObjectArray = new GameObject[10];

You can access it like so

gameObjectArray[0] = new GameObject("MyObject");
gameObjectArray[0].AddComponent<...>();
Destroy(gameObjectArray[0]);

Note that until you actually put a GameObject into the array you will get a null reference error if you try and access the element.