Trying to set A = 1, B = 2, etc.

As my title suggests, I’m looking to set a = 1, b = 2, etc and then write the letters to the TextMesh. Right now the code is writing 5 numbers, moving to a new line and writing “,”. What I’m wanting it to do is write the 5 numbers, move down a line, and then write the letters associated to the numbers on the above line, in the same order. I’ve been trying to accomplish this with an “if” statement inside of a method, that I would then call in the “for” loop. Any help sorting this out would be appreciated.

using UnityEngine;
using System.Collections;

public class Code_ABC012 : MonoBehaviour
{
  // Use this for initialization
  public int index = 5;

  void Start()
  {
  int numbers = 0;
  string resultsN = "", resultsL = "";
  string lett = "";
  string letter = lettersClass(lett, numbers);

  //Loops through the set index to display [index] amount of random numbers.
  for (int i = 0; i < index; i++)
  {
  if (i == 0)
  {
  numbers = Random.Range(1, 26);
  resultsN += numbers.ToString();

  resultsL += letter.ToString();
  }
  else
  {
  numbers = Random.Range(1, 26);
  resultsN += ", " + numbers.ToString();
  resultsL += ", " + letter.ToString();
  }
  }   

  GetComponent<TextMesh>().text = resultsN + "\n" + resultsL;
  }

  public string lettersClass (string let, int num)
  {
  if (num == 1) { let = "a"; }
  else if (num == 2) { let = "b"; }
  else if (num == 3) { let = "c"; }
  else if (num == 4) { let = "d"; }
  else if (num == 5) { let = "e"; }
  else if (num == 6) { let = "f"; }
  else if (num == 7) { let = "g"; }
  else if (num == 8) { let = "h"; }
  else if (num == 9) { let = "i"; }
  else if (num == 10) { let = "j"; }
  else if (num == 11) { let = "k"; }
  else if (num == 12) { let = "l"; }
  else if (num == 13) { let = "m"; }
  else if (num == 14) { let = "n"; }
  else if (num == 15) { let = "o"; }
  else if (num == 16) { let = "p"; }
  else if (num == 17) { let = "q"; }
  else if (num == 18) { let = "r"; }
  else if (num == 19) { let = "s"; }
  else if (num == 20) { let = "t"; }
  else if (num == 21) { let = "u"; }
  else if (num == 22) { let = "v"; }
  else if (num == 23) { let = "w"; }
  else if (num == 24) { let = "x"; }
  else if (num == 25) { let = "y"; }
  else if (num == 26) { let = "z"; }
  return let;
  }
   
  // Update is called once per frame
  void Update ()
  {
   
  }
}

before you go any further, please have a look at enums, Generic lists and switch statements. Trust me it will help you for this solution and many others.

also, please place your code in relevant functions and format it so that we can read it easier.

1 Like

Thank you for the advice. After looking at the enums, I now have the code working as inteaded

no problem, if you don’t mind, maybe consider posting it here to help someone else.

A dictionary is also a valid target for this.

For text you can also convert the characters to their Unicode values and do direct addition.

Try this code

using UnityEngine;
using System.Collections;

public class Letters : MonoBehaviour {


    public int index = 5;

    string alphabet = "abcdefghijklmnopqrstuvwxyz";

    string resultsN = "", resultsL = "";

    private void GetRandomNameYourFunction(string separator = "")
    {
        int numbers = Random.Range(1, 26);

        resultsN += separator + numbers.ToString();
       
        resultsL += separator + alphabet[numbers-1].ToString();
    }
    // Use this for initialization
    void Start () {

        GetRandomNameYourFunction();

        for(int i = 1; i < index; i++)
        {
            GetRandomNameYourFunction(", ");
        }

        Debug.Log(resultsN + "\n" + resultsL);
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}
1 Like