Can if loop be used to create variable names?

I would like to use my current if loop to generate names, if I have “p = 1 to 9” I would like to use the p from my current loop to generate a variable name for example planet1, planet2 etc. (planet + p = planet1)

string[] planet1;
string[] planet2;
string[] planet3;
string[] planet4;
string[] planet5;
string[] planet6;
string[] planet7;
string[] planet8;
string[] planet9;

if (sFolder) {  // Checks to see if there is a Satellites folder

				var numSat = sFolder.transform.childCount;
				satellites[0] = "" + numSat; // Adds Satellite amount at the given index
				if (numSat != 0) for (int s = 1; s <=9; s++){
					var sNameNo   = GameObject.Find("Galaxy" + cSystem + "/Planets/" + p + "/Satellites/" + s);
					if (sNameNo) {
						satelliteName = GameObject.Find("Galaxy" + cSystem + "/Planets/" + p + "/Satellites/" + s).GetComponent<Info>().Name;
						satellites ~~= satelliteName; // Adds Satellite at the given index~~

~~ var script = sNameNo.GetComponent();~~
~~ if (script == null) sNameNo.AddComponent(“sOrbitAndRotate”);}}~~
// for (int i = 0; i < satellites.Length; i++){ print (planetName + " satellites[" + i + "] = " + satellites*);}*
* if (p == 1) planet1 = satellites;*
* if (p == 2) planet2 = satellites;*
* if (p == 3) planet3 = satellites;*
* if (p == 4) planet4 = satellites;*
* if (p == 5) planet5 = satellites;*
* if (p == 6) planet6 = satellites;*
* if (p == 7) planet7 = satellites;*
* if (p == 8) planet8 = satellites;*
* if (p == 9) planet9 = satellites;*
* satellites = new string[10];*
As you can see its pretty much the same code, I like to save satellites to their planets before its reset. This code works fine, but its large. Imagine if I have more then 9 planets.
Id be nice if it looked something like this.
[planet+p] = satellites;} // or however you do this.
satellites = new string[10];

I’d use Linq to create a range of numbers, then use Generic List to create a list of names from the int array.

C# Example:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class Example : MonoBehaviour 
{
	int[] pNum;
	List<string> pName;
	
	void Start()
	{
		pName = new List<string>();
		pNum = Enumerable.Range(1, 100).ToArray();
		foreach(int num in pNum)
			pName.Add ("Planet "+num.ToString ());
		foreach(string name in pName)
			print (name);
	}
}

JS Example:

#pragma strict

import System.Collections.Generic;
import System.Linq;

private var pNum : int[] = Enumerable.Range(1, 100).ToArray();
private var pName : List.<String> = new List.<String>();

function Start()
{
	for(var num : int in pNum)
		pName.Add("Planet "+num.ToString());
	
	for(var name : String in pName)
		print(name);
}

Strictly speaking, yes, you can create new variables programmatically. But from what I can glean from your code this would be a particularly deranged use case.

Your organizational system makes sense, but the way you are accessing various pieces doesn’t make a lot of sense.

Galaxy

   Planet

      Satellite

Make each type if thing it’s own script, and use Unity - Scripting API: Component.GetComponentsInChildren to grab the sub pieces when you need to process them.

I’m not sure what else to suggest, apart from TOTALLY ABANDONING your current line of thinking. It will not end well.