- Home /
The question is answered, right answer was accepted
Random word from text file c#
I'm working on an app for my daughter and I'm freakin' dying over hear. I've been learning to code, so I'm not quite versed enough to figure this out. I've searched, watched tutorials, etc.
I have a simple text file called word list. I want to split it by line (\n) and then pick a random word, then pass that word on to my TextToSpeech class.
I love figuring this stuff out on my own, but I'm getting dangerously delirious. LOL. Can someone please show me the right way to write this? I would love you forever!!
Here is my current code that obviously does not work. I cant get the random word part right.
using UnityEngine;
using System.Collections;
public class GuiSampleController : MonoBehaviour {
public TextAsset WordList;
string WordToSpeak = WordList.text.Split('\n');
int MyRandom = Random.Range (1,101);
void Start ()
{
TextToSpeechManager.SendTextToSpeech(WordToSpeak[MyRandom]);
}
}
Thanks guys. Currently, it wont compile and I get this error - Assets/ScriptsAndScene/GuiSampleController.cs(8,31): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `GuiSampleController.WordList'
@Habitablaba Since we are talking about it, does the OP actually receive the messages we post in moderation? I read somewhere this is not the case. (And OPs never seem to reply.)
As far as I can tell, yes. Been using it pretty heavily over the past several days, and have even had conversations with people via send message while their question was in moderation.
The only unknown for me right now is what happens if you comment on a question and then reject it. Not sure if the user gets the response in that case.
I received emails that comments were made before it was pushed through. Thanks for trying to help.
Answer by artfish · Oct 28, 2014 at 01:58 AM
Ok. I finally got it.
using UnityEngine; using System.Collections;
public class textNewSpeaker : MonoBehaviour {
public TextAsset textFile;
string[] dialogLines;
void Start ()
{
if(textFile != null)
{
dialogLines = ( textFile.text.Split( '\n' ) );
string dialog = dialogLines[Random.Range(0,100)];
TextToSpeechManager.SendTextToSpeech(dialog);
//GetComponent<TextMesh>().text = dialog;
}
}
}
`
Answer by MrJamieMcC · Jul 20, 2017 at 09:49 PM
Rather than do string dialog = dialogLines[Random.Range(0,100)];
You should so string dialog = dialogLines[Random.Range(0,dialogLines.Length)];
.
The .Length part makes the array as big as how many values are in your document. In your instance if you have any thing less than 100 values in your document nothing will be displayed. Using .Length ensures the Random.Range function will only choose a number which has a value.
For example if your document has only 20 lines in it but Random.Range gets the number 58 then nothing will be displayed as their isn't a 58 value/line. Adding the dialogLines.Length
part means the max number can only be the max value amount of values in your document.
Unity may sometimes change your .Length to .Count. That is fine. They both work.
I know this question is old but i wanted to add this in case somebody else is having this issue.
Answer by AlwaysSunny · Oct 27, 2014 at 11:50 PM
Looks like you want WordToSpeak to be an array of strings
string[] WordToSpeak
In the future, bear in mind we'd like to hear any specific error messages you get to help us help you. Best of luck,
Here's how I split a huge list of words and updated a Text$$anonymous$$esh with a random word in JS and it works great. I want to do the same thing in C# and send the word to the c# Class TextToSpeech$$anonymous$$anager.
public var fileContents : TextAsset;
var mydata = fileContents.text.Split("\n"[0]);
var myrandom = Random.Range(1,100000);
function Start () { GetComponent(Text$$anonymous$$esh).text = mydata[myrandom];`}
Thanks for trying to point me in the right direction.
Follow this Question
Related Questions
How do I split a rectangle into random smaller rectangles? 1 Answer
HELP! Make text appear in scene after 5 seconds? 2 Answers
How to save script component variables at runtime from IOS device? 1 Answer
Random Text String 2 Answers
Splitting Text 2 Answers