Creating a list of game objects to move across screen

Hi all, I am new to unity and c#, I have been learning the basics over the last month. I have been trying to work on a small word game, a word is to be displayed with missing letters and then letters move along the screen and player has to choose the right letters to complete the word.

I am looking for advice on what would be the best way to do this. Through research I think I can make a list/array of words, shuffle and then remove random letters. I have made 26 graphics, alphabet, can I tag each letter i.e, a, b etc and then create an array of them shuffle and then move across the screen which can be selected by player? I am guessing I will need to tie the arrays together. Advice on what I should research further if you could point me in the right direction would be appreciated, many thanks.

You can do that with 2D textures or 3D objects. 2D is easier, because you can think in terms of screen coordinates - you can use an array of textures and the GUI system. 3D is more complicated, but allows more special effects. I’ll give you some hints about the 2D version, and you can in the future extend them to a 3D version.

The character graphics: you could create a public Texture2D array letters in the script, and populate it in the Inspector with the textures so that letter[0] is “A”, letter[1] is “B” and so on - this helps finding a given letter’s texture by its ASCII code:

public Texture2D[26] letters; // drag the textures orderly here

// this function returns the texture corresponding to the letter:
Texture2D GetLetter(char letter){
  return letters[(int)letter - (int)'A'];
}

The words: you could create a public array of strings and populate it in the Inspector, like in the graphics case:

public string[] words;

char and string are different things in C#, thus you must use functions to convert from one to the other when necessary. If you want to “print” some word with your letters, for instance, you can use something like this:

string curWord = "Hello"; // word to print
Vector2 pos = new Vector2(10,10); // where to draw the word
int charWidth = 40; // fixed character width
int charHeight = 60; // character height
int charSpace = 5; // space between characters

void OnGUI(){
  char[] chars = curWord.ToCharArray();
  Rect r = new Rect(pos.x, pos.y, charWidth, charHeight);
  foreach (char c in chars){
    GUI.DrawTexture(r, GetLetter(c)); // draw the letter c
    r.x += charWidth + charSpace; // advance screen position
  }
}

NOTE: GUI items are rendered in the function OnGUI. This function is called automatically by the system - don’t call OnGUI yourself, just create the function!

If you want to display clickable letters, use GUI.Button instead:

  foreach (char c in chars){
    if (GUI.Button(r, GetLetter(c))){
      // the character c was clicked! do whatever
      // you have to do with it here
    }
    r.x += charWidth + charSpace; // calculate next char position
  }