- Home /
String and For() question
So I am making a typing ish type of script (I word it like that because I'm trying to help people with something) but to do that, I need help, I got a part of a script :
string[] Alphabet = new string[26] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
for (int i = 0; i < amountOfCharacters; i++) //I = 0, For every "1" that amountCharacters, add "1" to the I. Until the I is equal to "amountOfCharacters"...
{
string CharacterCom1 = Alphabet[Random.Range(0, Alphabet.Length)];
}
}
Now what I am wanting to do is, say amountOfCharacters is "5" so it would input 4 times. (because of the = part)
For the 4 random letters, Is there any way I can identify them ?
I.e. say it gives me "A,B,C,D"
Anyway to locate that A,B,C,D elsewhere?
I can help, but I need some more information. 1. Are you trying to get the amount of chars in a string? If so, I suggest using string.Length. 2. To compare chars from a string to other chars is as simple as turning the string into an array and referencing the specfic part or running a foreach loop on the chars in the string. 3. Are you asking how to randomize the output of your characters? 4. If your alphabet is only single characters, you should switch it from string[] to char[].
@RobAnthem it's like a typing game, So I want it so whatever A,B,C,D is It is a string or something.
What you have looks pretty good for generating the string, but the problem is that it's replacing the entire string each time rather than concatenating it:
string stringResult="";
for (int i = 0; i < amountOfCharacters; i++)
{
stringResult += Alphabet[Random.Range(0, Alphabet.Length-1)]; //the += operator when used with a string, performs a concatenation. (also- don't forget to subtract 1 from the length, or your index could be too large)
}
@Glurth, Random.Range doesn't include last value, so no need for -1
You don't need to subtract one from the length since the max value of the int version of random range is exclusive
@Glurth In all due the respect, I think I worded it weirdly. To which I apologize (autistic)
So let's try wording it like this :
What I want to do do, is say "amountOfCharacters" is 3, It will then generate 2 letters. Because of the string and random.range and all that
Now whatever those two letters are, I was wondering if there is any way to reference them.
To be exact, what I want to do is be able to display those two letters on the screen for it to be typed. But that display letters on the screen portion, I can do on my own, what I need to do, is be able to reference those two letters in another function. Not just in the "for" statement.
If that makes sense.
I'm not an expert, in fact i considere myself to be a noob in progra$$anonymous$$g. Out of that, i wonder why if you need to store those randomly generated values, you dont do that on the for loop itself.
There is a couple of ways to do that, but this depends extremely on what exactly is your scenario. You can just create a new list of strings, and store it on a serializable variable. You can to, store the index of the generated lether on another list of index's. But this depends on what are your limitations. $$anonymous$$ay be you want to store more than one list of strings and that is of course something beyond my understandings. I know there are tricky ways to store list of list's ut I still did not get to that part of my training.
But if you just need to store one randomly generated string, it should not be too dificult. I never did something like this but for what i have read, if you just need to store that generated leathers for displaying them, you can add them together on just one string, and make the separations inside that string for example
String = new String ("A, B, C, D");
Insted of
string[] = new string[4] { "A", "B", "C", "D" };
As i said before, i never tryied and never do such a thing. But there are some ideas of what you have to try before, trying to do something too complicated.
Hope you dont lose your time reading me.
Have a nice new year!
I don't know what the result will be though is the problem...
You as well with a nice new year.
Answer by RobAnthem · Jan 01, 2017 at 08:44 PM
Oh! Just make it pass the reference on to something else you can use, like a Static file, or a MonoBehaviour. For a static file you could just do
GameData.alphaString = stringResult;
or for a MonoBehaviour
FindObjectOfType<myObject>().alphaString = stringResult;
Then from those scripts you can use it, or pass it on, or whatever.
Did not work, this is on a empty with just this script..
Answer by Zarteke · Jan 03, 2017 at 10:02 PM
If you want to reference it outside of the for loop(and outside of this script it seems) you should just declare a variable to hold your results and then assign it during your loop:
public string MyString; // This is where your results will be
//This is your method
public void MyMethod()
{
//(...)
string[] Alphabet = new string[26] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
for (int i = 0; i < amountOfCharacters; i++)
MyString += Alphabet[Random.Range(0, Alphabet.Length)];//MyString will look like "ABCD"
}
You can access your results individually by calling MyString[x] where x < MyString.Lenght.
Answer by gcoope · Jan 12, 2017 at 02:02 PM
I feel like you just need to store the randomly chosen letters into an array or list. Something like:
public string[] chosenLetters;
public int amountOfCharacters = 4; // Or however many you want to generate
private string[] Alphabet = new string[26] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
public void GenerateRandomLetters() {
chosenLetters = new string[amountOfCharacters];
for (int i = 0; i < amountOfCharacters; i++) {
chosenLetters[i] = Alphabet[Random.Range(0, Alphabet.Length)];
}
}
With this code you can call GenerateRandomLetters method, and then reference the chosenLetters array that will contain the amount of randomly chosen letters that you want.
Another way to makes this more elegant is to have the GenerateRandomLetters return an array of letters directly by changing void to string[] and adding
return ChosenLetters;
to the end.
An even nicer addition would be to pass the amount of characters you want to generate as a parameter in the GenerateRandomLetters method.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
C# Variable 4 Answers
Can i give GetComponent a variabel instead of a scriptname? 1 Answer
Problem recognizing declaration of array of strings 1 Answer