Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Jul 20, 2017 at 10:42 PM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by artfish · Oct 28, 2014 at 01:05 AM · textrandomsplitcsword

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]);
         }
     }
 
 
Comment
Add comment · Show 4
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 artfish · Oct 28, 2014 at 12:58 AM 0
Share

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'

avatar image Unitraxx · Oct 28, 2014 at 01:12 AM 0
Share

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

avatar image Habitablaba · Oct 28, 2014 at 01:15 AM 0
Share

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.

avatar image artfish · Oct 28, 2014 at 01:18 AM 0
Share

I received emails that comments were made before it was pushed through. Thanks for trying to help.

3 Replies

  • Sort: 
avatar image
3
Best Answer

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;
         }
     }
 }
 



`

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
2

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.

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 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,

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 artfish · Oct 27, 2014 at 11:55 PM 0
Share

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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