Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JeffHardddyyy · Dec 31, 2016 at 11:55 PM · c#stringrandom.rangefor

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?

Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RobAnthem · Jan 01, 2017 at 12:52 AM 0
Share

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[].

avatar image JeffHardddyyy RobAnthem · Jan 01, 2017 at 04:26 AM 0
Share

@RobAnthem it's like a typing game, So I want it so whatever A,B,C,D is It is a string or something.

avatar image Glurth · Jan 01, 2017 at 05:39 AM 0
Share

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)
  }
avatar image IgorAherne Glurth · Jan 01, 2017 at 10:19 AM 0
Share

@Glurth, Random.Range doesn't include last value, so no need for -1

avatar image Bunny83 Glurth · Jan 01, 2017 at 10:24 AM 0
Share

You don't need to subtract one from the length since the max value of the int version of random range is exclusive

avatar image JeffHardddyyy Glurth · Jan 01, 2017 at 03:58 PM 0
Share

@Glurth What you did unfortunately did not work. In addition to the "-1" thing

I got it from Scribe here

Show more comments
avatar image JeffHardddyyy · Jan 01, 2017 at 08:37 PM 0
Share

@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.

avatar image UrielKane JeffHardddyyy · Jan 02, 2017 at 06:45 AM 0
Share

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!

avatar image JeffHardddyyy UrielKane · Jan 02, 2017 at 03:32 PM 0
Share

I don't know what the result will be though is the problem...

You as well with a nice new year.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JeffHardddyyy · Jan 02, 2017 at 03:31 PM 0
Share

Did not work, this is on a empty with just this script..

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

268 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges