Arrays & Lists in C# for Unity

Karuna Ketan
2 min readDec 21, 2022

--

I learned about Arrays & Lists in Unity…

15th Programs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ArrayExample : MonoBehaviour
{
// Example - 1
int _number = 12;
int[] _numberArray = new int[4]; // Declaring Array

// Example - 2
string[] _names = { "Elon", "Mark", "Sundar", "Albert" }; // Assigning values in array

// Example - 3
public GameObject[] _players; // Public // Array of GameObject type
// [SerializeField] GameObject[] _players; // Private

// Start is called before the first frame update
void Start()
{
_numberArray[0] = 10;
_numberArray[1] = 23;
_numberArray[2] = 53;
_numberArray[3] = 15;

//Debug.Log(_numberArray[0]);
_players = GameObject.FindGameObjectsWithTag("Player");
}

// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
_players = GameObject.FindGameObjectsWithTag("Enemy");
}
}
}

From above program:

void Start()
{
_numberArray[0] = 10;
_numberArray[1] = 23;
_numberArray[2] = 53;
_numberArray[3] = 15;
//Debug.Log(_numberArray[0]);
_players = GameObject.FindGameObjectsWithTag("Player");
}
_players = GameObject.FindGameObjectsWithTag("Player");
  • The GameObject having Tag named Player will get assigned to GameObject array named _players…
if (Input.GetKeyDown(KeyCode.Space))
{
_players = GameObject.FindGameObjectsWithTag("Enemy");
}
  • If space key is pressed then it is going to take _players array and is going to reassign with all the different GameObjects that has a tag as Enemy insted…
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ListExample : MonoBehaviour
{
List<string> _numbers = new List<string>();

// Start is called before the first frame update
void Start()
{
_numbers.Add("Elon"); // --0
_numbers.Add("Mark"); // --1
_numbers.Add("Sundar"); // --2
_numbers.Add("Albert"); // --3

_numbers.Remove("Elon");

_numbers.Insert(1, "Arjun");

Debug.Log(_numbers[0]);

Debug.Log(_numbers[1]);

Debug.Log(_numbers[2]);

// Debug.Log(_numbers[3]);
}

// Update is called once per frame
void Update()
{

}
}

From above program:

List<string> _numbers = new List<string>();
  • This is how we can instantiate a list… This is not actually a datatype. This is a class that exits someware inside System.Collections.Generic…
_numbers.Remove("Elon");
  • This removes Elon and now the 0 index of list named _numbers will become Mark…
_numbers.Insert(1, "Arjun");
  • This will insert Arjun at 1 index i.e. _numbers.Remove(“Elon”); will remove Elon, then 0 index will be Mark then 1 index will be Arjun as we are inserting at 1, 2 index will be Sundar and so on…
Debug.Log(_numbers[0]);
  • This grabs the 0 index of list named _numbers i.e Elon…And same as Debug.Log(_numbers[1]); will grab the 1 index of list named _numbers i.e Mark…and so on…

That’s all…Thank you for reading😉:)

click here to follow me in LinkedIn for more updates…

click here to follow me in GitHub for more updates…

--

--

Karuna Ketan

Game Designer and Developer | Unity, Unreal Engine, Blender