Naming a Variable, Based on other Variables?

Hey!

So, I’m not sure this is possible, but I’m hoping - if it is, someone here might have a solution, I’ve done some googling, but either the information isn’t out there (probably meaning it’s impossible) or I’m just googling with the wrong words…

What I’m trying to do, is have a “GameObject Variable” named, the same thing that the In-Game Object is named, but… I don’t exactly want to type out (not that it would be an issue, I just figure it would make my code “nicer” to not do so) “GameObject H1 = GameObject.Find(“H1”);” with varied names over and over.

So far, I have got this far in the code “GameObject H1 = GameObject.Find(Alpha + Numeral.ToString());” - which works - but doesn’t quite do the next part I’m hoping for, which would be " new GameObject Alpha + Numeral.ToString() = GameObject.Find(Alpha + Numeral.ToString()); is there anyway to do this?

I will paste my raw code beneath - just so you can see (easier) what I’m going for…

using UnityEngine;
using System.Collections;

public class PieceCreator : MonoBehaviour {

    private int x = 1;

    private string alpha;
    private int numeral;

    void Awake()
    {
        
    }

    void Start()
    {
        for(int i = 1; i >= 16; i++)
        {
            AlphaName();
            NumeralName();
            GameObject tempBlack = GameObject.Find(alpha + (numeral.ToString()));
            GameObject + numeral.ToString() = tempBlack;
            x++;       
        }
    }

    void AlphaName()
    {
        if (x == 1)
        {
            alpha = "H";
        }
        if (x == 2)
        {
            alpha = "H";
        }
        if (x == 3)
        {
            alpha = "H";
        }
        if (x == 4)
        {
            alpha = "H";
        }
        if (x == 5)
        {
            alpha = "H";
        }
        if (x == 6)
        {
            alpha = "H";
        }
        if (x == 7)
        {
            alpha = "H";
        }
        if (x == 8)
        {
            alpha = "H";
        }
        if (x == 9)
        {
            alpha = "G";
        }
        if (x == 10)
        {
            alpha = "G";
        }
        if (x == 11)
        {
            alpha = "G";
        }
        if (x == 12)
        {
            alpha = "G";
        }
        if (x == 13)
        {
            alpha = "G";
        }
        if (x == 14)
        {
            alpha = "G";
        }
        if (x == 15)
        {
            alpha = "G";
        }
        if (x == 16)
        {
            alpha = "G";
        }


    }

    void NumeralName()
    {
        numeral = x;
        Debug.Log("numeral = " + numeral.ToString());
    }
}

Thank you in advance - if anything I have said is unclear, I can try to re-explain, but unfortunately I don’t know all the “proper jargon”, sorry if I’m really confusing!

I’m typically not one for handing out code, but this is probably a good way to teach some more intermediate programming concepts - you really need to get knee deep into this kind of stuff immediately since you’re making a game.

Here is some basic concepts translated into a usable class for Unity, with a ton of comments:

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

public class PieceCreator : MonoBehaviour {

    /*
    PLEASE KEEP IN MIND:
    This kind of structure is NOT usually good practice for games!
    */


    // This is a private dictionary which will hold all of the chest pieces we find in Start()
    // You can call ChessPieces like an array: ChessPieces["G1"] to get the object with a key "G1".
    Dictionary<string, GameObject> ChessPieces = new Dictionary<string, GameObject>();

    void Start() {
        /*
        This is a typical for loop, starting at an index of 0.
        In this case, i will reach 15 and then quit. If we include 0, it runs a total of 16 loops.
        */
        for (int i = 0; i < 16; i++) {
            
            

            // Instead of your entire AlphaName() function, we can do two things here:
            if (StoreInMemory) { // If we want to store the prefix (you call it alpha) in memory, we can do this:
                
                // We can assign a value in memory to pass through Find like so:

                string prefix = "H" + i.ToString();
                if (i > 8) prefix = "G" + (i - 8).ToString(); // I subtract 8 from i so that the list of G pieces is: G1, G2, G3... etc

                // And after determining if it should be H or G, pass it into GameObject.Find and retrieve the Current Piece.
                // We also store this in memory.

                GameObject currentPiece = GameObject.Find(prefix);

                // Then we add the current piece, which we've found and stored in memory, to the Dictionary:
                ChessPieces.Add(prefix, currentPiece);
            }
            else { // Or we can decide to not store the prefix in memory

                if (i > 8) ChessPieces.Add("G" + i.ToString(), GameObject.Find("G" + i.ToString()));
                else ChessPieces.Add("H" + (i - 8).ToString(), GameObject.Find("H" + (i - 8).ToString())); // Same deal with the subtraction as above

                // Why would you want to have "more code"?
                // Well, RAM is much more important than file size.
                // Although more code directly correlates to more space on the hard drive,
                // It can sometimes reduce the amount of RAM you use.
                //
                // Also, its unnecessary to store it as the code that you have to repeat is small and managable.
                // You're also not using that value again after this case.
                //
                // TL;DR its best practice in normal software and small games to use as little RAM as possible while still keeping managable code that isn't tedious.

                /*
                PLEASE NOTE: In a REAL video game that isnt as simple as Chess, this thought process is TOO SIMPLE!
                In much bigger projects, you have to manage ALL of your resources and consider everything very carefully.
                In bigger games, its OKAY to use more RAM and take up a lot of file space, its often expected!
                And in those cases, it DOES make sense to use less code and more RAM, because you'll end up using less CPU.

                tl;dr in bigger projects in games, prioritze CPU over everything else. RAM comes second, file size comes third.
                */
            }
        }
    }
}