How to dynamically create variable names?

Hey folks, Unity beginner here. Having a blast so far with learning the ropes, and following the tutorials. However, I’ve run into a problem that I’m not sure how to effectively Google, so here goes:

Background Info: I’m trying to program a game like “Tetris Attack” where differently colored blocks are instantiated in a grid, and those blocks can later be moved around and switched, and if you manage to get three in a row, they disappear.

So as you can see, I’m having to individually spell out each block, creating new variables for each Vector, for each color, and for each GameObject. This works, but considering there will later be around 100+ blocks in the game being constantly destroyed and created, I figure this is a pretty wasteful way to go about it.

My intuition would be to create a for loop, with “counter” variables that let me dynamically create the variable names I need as I go along (A1, A2, A3, etc). But annoyingly, I can’t name a variable something that consists of another variable :confused:

Here is an excerpt of this exhausting naming and instantiating process. I’m assuming there is a better practice, and I’d be grateful for any pointers!

		Vector2 A1 = new Vector2 (0, 0);
		int A1color = Random.Range (0, 5);
		GameObject A1new = Instantiate (colors [A1color], A1, Quaternion.identity);			
		Gridspots.Add ("A1", A1new);
		Colorspots.Add ("A1", new colorclass (A1color));

		Vector2 A2 = new Vector2 (0, 1);
		int A2color = Random.Range (0, 5);
		GameObject A2new = Instantiate (colors[A2color], A2, Quaternion.identity);
		Gridspots.Add ("A2", A2new);
		Colorspots.Add ("A2", new colorclass (A2color));

		Vector2 A3 = new Vector2 (0, 2);
		int A3color = Random.Range (0, 5);
		GameObject A3new = Instantiate (colors[A3color], A3, Quaternion.identity);
		Gridspots.Add ("A3", A3new);
		Colorspots.Add ("A3", new colorclass (A3color));

		Vector2 B1 = new Vector2 (1, 0);
		int B1color = Random.Range (0, 5);
		GameObject B1new = Instantiate (colors[B1color], B1, Quaternion.identity);
		Gridspots.Add ("B1", B1new);
		Colorspots.Add ("B1", new colorclass (B1color));

		Vector2 B2 = new Vector2 (1, 1);
		int B2color = Random.Range (0, 5);
		GameObject B2new = Instantiate (colors[B2color], B2, Quaternion.identity);
		Gridspots.Add ("B2", B2new);
		Colorspots.Add ("B2", new colorclass (B2color));

et cetera!

You can’t create variable names at runtime in a compiled language like c#/c++. What you are looking for is a way to dynamically allocate an arbitrary number of objects which you can point to using one variable name and an index.

This is what Arrays and Lists are used for. In your case, you will benefit from creating a Class that will have the information you need for every shape you spawn and then create instances of this class, which you then can store in an array or list.