• 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
Question by Kasigawa · Jan 07, 2014 at 05:31 AM · javascriptbuggenerator

Why isn't my word generator working?

I'm not a good coder; nevertheless, I was wondering if anyone could be so generous as to help me figure out why my code won't run. The script is a word generator intended to make a made-up language for an interactive comic book. I just can't seem to figure out how to make the script work. I hope someone can help me. It should be a relatively simple script to write, yet I find myself struggling tremendously. In the script, I went as far as to include notes to make it easier to understand. If the word generator works, I would gladly let others use it. (Please don't be daunted by it's length. It's long because I reused the switch statement for all of it's 8 parts)

 #pragma strict
 var wordCount : int = 10; //set this to desired amount of words (when the code works); use 10 for testing purposes. 
 
 
 function Start () {
 
     //get number of words you want to make.
     for (var x = 0; x< wordCount; x++){
         wordGenerator(); //execute wordGenerator function to generate words.
     }
 }
 Debug.Log(word);//print words on screen. 
 
 
 
 
 
 
 
 
 
 
 //wordGenerator function
 
 function wordGenerator (){
     //arrays
     var commons : char ["a","i","u","e","o","ka","ki","ku","ke","ko","sa","shi","su","se","so","ta","c$$anonymous$$","tsu","te","to","na","ni","nu","ne","no","ha","hi","fu","he","ho","ma","mi","mu","me","mo","ya","yu","yo","ra","ri","ru","re","ro","wa","n"];
     var all : char ["a","i","u","e","o","ba","bi","bu","be","bo","da","di","du","de","do","fa","fi","fu","fe","fo","ga","gi","gu","ge","go","ha","hi","hu","he","ho","ja","ji","ju","je","jo","ka","ki","ku","ke","ko","la","li","lu","le","lo","ma","mi","mu","me","mo","na","ni","nu","ne","no","pa","pi","pu","pe","po","ra","ri","ru","re","ro","sa","si","su","se","so","ta","ti","tu","te","to","va","vi","vu","ve","vo","wa","wi","wu","we","wo","xa","xi","xu","xe","xo","ya","yi","yu","ye","yo","za","zi","zu","ze","zo"];
     var addOns : char ["à","è","ì","ò","ù","á","é","í","ó","ú","â","ê","î","ô","û","ā","ī","ū","ē","ō","ã","ĩ","ũ","ẽ","õ","ă","ĭ","ŭ","ĕ","ŏ","ȃ","ȋ","ȗ","ȇ","ȏ","°"];
     var endAddOn : char ["ll","s","ss","id","ia","ing","ung","eng","ong"];
     //variables
     var nothing : char = " "; //space varable.
     var word : char; //the function will return this as a full word at the end of the function. 
     
     //part variables (not sure it should be a int or char).
     var p1 : char;
     var p2 : char;
     var p3 : char;
     var p4 : char;
     var p5 : char;
     var p6 : char;
     var p7 : char;
     var p8 : char;
     /*
     var t1 : int; //incase I need a var to get a part variable as a number and convert it using an integer to string statment. T stands for tempuary. 
     var t2 : int;
     var t3 : int;
     var t4 : int;
     var t5 : int;
     var t6 : int;
     var t7 : int;
     var t8 : int;
     */
     var ranNum : int; //random number variable for the switch statments. 
     
     
     
     ranNum = Math.random(1,100);//gets a random number from 1 to 100 for the switch statment. The 1-100 will also act as my percentages(1%-100%) for the list and random letters in an arrays to select a syllable for a particular part.
     
     
     //get p1; all parts (p2-p8)have the same code except for part1. 
     
     p1 = switch(ranNum){
             case 1:
                 if (ranNum > 30){
                       p1 = Math.random()*(commons.length-1); //get a random number(or char) number from the commons array.
                   }
             break;
             case 2:
                 if (ranNum >40){
                       p1 = Math.random()*(all.length-1);//get a random number(or char) from the all array.
                   }
              break;
              case 3:
                  if (ranNum >60){
                       p1 = Math.random()*(addOns.length-1);//get a random number(or char) from the addOns array.
                   }
              break;
         }
         
         
     ranNum = Math.random(1,100);    
         
     // get P2
         p2 = switch(ranNum){
             case 1:
                 if (ranNum > 30){
                       p2 = Math.random()*(commons.length-1);
                   }
             break;
             case 2:
                 if (ranNum >40){
                       p2 = Math.random()*(all.length-1);
                   }
              break;
              case 3:
                  if (ranNum >60){
                       p2 = Math.random()*(addOns.length-1);
                   }
               break;
               case 4:
                   if (ranNum >70){
                      p2 = Math.random()*(endAddOn.length-1); //get a random number(or char) from the endAddOn array.
                  }
               break;
             case 5:
                 if (ranNum >90){
                       p2 = nothing; //insert a space (after a digit gets a space all other parts get a space.
                   }
             break;
         }
     
     
     ranNum = Math.random(1,100);
     
     //get p3
     
         p3 = switch(ranNum){
             case 1:
                 if (ranNum > 30){
                       p3 = Math.random()*(commons.length-1);
                   }
             break;
             case 2:
                 if (ranNum >40){
                       p3 = Math.random()*(all.length-1);
                   }
              break;
              case 3:
                  if (ranNum >60){
                       p3 = Math.random()*(addOns.length-1);
                   }
               break;
               case 4:
                   if (ranNum >70){
                      p3 = Math.random()*(endAddOn.length-1);
                  }
               break;
             case 5:
                 if (ranNum >90){
                       p3 = nothing;
                   }
             break;
         }
         
         if (p2 = nothing){
             p3 = nothing;        //if part before this one gets a space (nothing variable), then it also gets a space; include all the other parts.I mentioned this before in part1's switch statment.  
         }
         
         
     ranNum = Math.random(1,100);    
         
     //get p4
         p4 = switch(ranNum){
             case 1:
                 if (ranNum > 30){
                       p4 = Math.random()*(commons.length-1);
                   }
             break;
             case 2:
                 if (ranNum >40){
                       p4 = Math.random()*(all.length-1);
                   }
              break;
              case 3:
                  if (ranNum >60){
                       p4 = Math.random()*(addOns.length-1);
                   }
               break;
               case 4:
                   if (ranNum >70){
                      p4 = Math.random()*(endAddOn.length-1);
                  }
               break;
             case 5:
                 if (ranNum >90){
                       p4 = nothing;
                   }
             break;
         }
         if (p3 = nothing){
             p4 = nothing;
         }
         
         
     ranNum = Math.random(1,100);    
                 
     //get P5
         p5 = switch(ranNum){
             case 1:
                 if (ranNum > 30){
                       p5 = Math.random()*(commons.length-1);
                   }
             break;
             case 2:
                 if (ranNum >40){
                       p5 = Math.random()*(all.length-1);
                   }
              break;
              case 3:
                  if (ranNum >60){
                       p5 = Math.random()*(addOns.length-1);
                   }
               break;
               case 4:
                   if (ranNum >70){
                      p5 = Math.random()*(endAddOn.length-1);
                  }
               break;
             case 5:
                 if (ranNum >90){
                       p5 = nothing;
                   }
             break;
         }
         if (p4 = nothing){
             p5 = nothing;
         }
         
     ranNum = Math.random(1,100);    
         
         
     //get p6
         p6 = switch(ranNum){
             case 1:
                 if (ranNum > 30){
                       p6 = Math.random()*(commons.length-1);
                   }
             break;
             case 2:
                 if (ranNum >40){
                       p6 = Math.random()*(all.length-1);
                   }
              break;
              case 3:
                  if (ranNum >60){
                       p6 = Math.random()*(addOns.length-1);
                   }
               break;
               case 4:
                   if (ranNum >70){
                      p6 = Math.random()*(endAddOn.length-1);
                  }
               break;
             case 5:
                 if (ranNum >90){
                       p6 = nothing;
                   }
             break;
         }
         if (p5 = nothing){
             p6 = nothing;
         }
         
     ranNum = Math.random(1,100);    
         
                 
     //get p7
         p7 = switch(ranNum){
             case 1:
                 if (ranNum > 30){
                       p7 = Math.random()*(commons.length-1);
                   }
             break;
             case 2:
                 if (ranNum >40){
                       p7 = Math.random()*(all.length-1);
                   }
              break;
              case 3:
                  if (ranNum >60){
                       p7 = Math.random()*(addOns.length-1);
                   }
               break;
               case 4:
                   if (ranNum >70){
                      p7 = Math.random()*(endAddOn.length-1);
                  }
               break;
             case 5:
                 if (ranNum >90){
                       p7 = nothing;
                   }
             break;
         }
         if (p6 = nothing){
             p7 = nothing;
         }
         
     ranNum = Math.random(1,100);    
         
         
     //get p8
             p8 = switch(ranNum){
             case 1:
                 if (ranNum > 30){
                       p8 = Math.random()*(commons.length-1);
                   }
             break;
             case 2:
                 if (ranNum >40){
                       p8 = Math.random()*(all.length-1);
                   }
              break;
              case 3:
                  if (ranNum >60){
                       p8 = Math.random()*(addOns.length-1);
                   }
               break;
               case 4:
                   if (ranNum >70){
                      p8 = Math.random()*(endAddOn.length-1);
                  }
               break;
             case 5:
                 if (ranNum >90){
                       p8 = nothing;
                   }
             break;
         }
         if (p7 = nothing){
             p8 = nothing;
         }
     
     
     //combine all parts to form a word and return it so that it can go back to it's original module to be printed to the screen. 
     
     word = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8; 
     return word;
 }
 
 
     
Comment

People who like this

0 Show 6
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 Benproductions1 · Jan 07, 2014 at 05:35 AM 0
Share

You have a lot of duplicate code in there, you could clean it up easily if you used some functions.

avatar image Benproductions1 · Jan 07, 2014 at 05:37 AM 0
Share

When you say: "It isn't working", that's completely useless information! What isn't working? Do you get any compiler errors? (you should) What have you done to fix it? Why do you think it's not working the way you want it to work? What is it currently doing that it shouldn't do?

All of these are questions you should consider answering when asking questions. Otherwise we're just guessing...

avatar image HappyMoo · Jan 07, 2014 at 05:37 AM 0
Share

do you mind if I switch this to C# from Javascript?

Also, do you really want all words to be made from 8 parts? That will give you very long words and you will miss all the nice short ones or is this what you try to do with the space? If one is space, next will be space too, so they wont be executed?

And why are you putting endAddons into the word?

Can you summarize how you want to construct your words in simple english without trying to explain the technical parts?

avatar image Benproductions1 · Jan 07, 2014 at 08:00 AM 0
Share

@HappyMoo, you shouldn't edit other peoples questions except for formatting or other, non-destructive corrections. Usually the problem that person has is in the code, so converting it won't just confuse the OP but will also hide the initial issue :)

avatar image HappyMoo · Jan 07, 2014 at 08:12 AM 0
Share

Oh I didn't meant to touch his question... just convert his code... as an answer...

But seeing his other question are also Javascript, that would probably confuse more than help

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by HappyMoo · Jan 07, 2014 at 08:27 AM

@Kasigawa, I created the randomPick function to allow you to express your word generation ideas in a more declarative way and then I created a generateWord function that I think is in the spirit of what you wanted to do, but may deviate from what you actually wrote. It should be way easier now to customize this to what you actually want exactly

 #pragma strict
 private var wordCount : int = 100;
 
 private var commons : String[] = ["a","i","u","e","o","ka","ki","ku","ke","ko","sa","shi","su","se","so","ta","c$$anonymous$$","tsu","te","to","na","ni","nu","ne","no","ha","hi","fu","he","ho","ma","mi","mu","me","mo","ya","yu","yo","ra","ri","ru","re","ro","wa","n"];
 private var all : String[] = ["a","i","u","e","o","ba","bi","bu","be","bo","da","di","du","de","do","fa","fi","fu","fe","fo","ga","gi","gu","ge","go","ha","hi","hu","he","ho","ja","ji","ju","je","jo","ka","ki","ku","ke","ko","la","li","lu","le","lo","ma","mi","mu","me","mo","na","ni","nu","ne","no","pa","pi","pu","pe","po","ra","ri","ru","re","ro","sa","si","su","se","so","ta","ti","tu","te","to","va","vi","vu","ve","vo","wa","wi","wu","we","wo","xa","xi","xu","xe","xo","ya","yi","yu","ye","yo","za","zi","zu","ze","zo"];
 private var addOns : String[] = ["à","è","ì","ò","ù","á","é","í","ó","ú","â","ê","î","ô","û","ā","ī","ū","ē","ō","ã","ĩ","ũ","ẽ","õ","ă","ĭ","ŭ","ĕ","ŏ","ȃ","ȋ","ȗ","ȇ","ȏ","°"];
 private var endAddOn : String[] = ["ll","s","ss","id","ia","ing","ung","eng","ong"];
 
 private var words : Array = new Array(); 
  
 function Start () {
     for (var x = 0; x< wordCount; x++){
         var word = generateWord();
         print(word);
         words.push(word);
     }
 }
 
 function generateWord (){
 
     var word:String = "";
 
      for(var i:int = 0; i<8; i++) // up to 8 word parts
      {
          word += randomPick([
              60, commons,
              30, all,
              10, addOns
          ]);
         if (i>0 && Random.value < 0.2) break; // 20% chance to stop here in all but the first step
      }
      
     word += randomPick([ // 50% chance of adding endAddOn at the end
         50, endAddOn,
         50, ""
     ]);
  
     return word;
 }
  
 // arguments come in pairs of probability and choice, like randomPick([40,"A", 10,"B"])
 // Probabilities don't need to add up to 100% - their relative size is important.
 // The example above picks "A" 80% of the time and "B" 20% of the time
 // Another Example: randomPick([1,"Third", 2,"TwoThirds"])
 // If the choice is an Array, it will pick one of the elements randomnly:
 // randomPick([2, ["John","Luke"], 6,"Anna"]). This will pick Anna 3 times as often as one of the other Names,
 // i.e. 25% (John or Luke), 75% Anna or completelly decoded: 12.5% John, 12.5% Luke, 75% Anna
 function randomPick(args:Object[]) : Object
 {
   var pSum:float = 0;
   for(var i:int = 0; i<args.length; i+=2) pSum += parseFloat(args[i].ToString());
   var p:float = Random.Range(0f, pSum);
   var runningP:float = 0f;
   for(i = 0; i<args.length; i+=2)
   {
       runningP += parseFloat(args[i].ToString());
       if (p<=runningP)
       {
           var ret:Object = args[i+1];
           // Too many array types in UnitScript
           if (typeof(ret)==typeof(Array))
               return (ret as Array)[Random.Range(0, (ret as Array).length)];
           if (typeof(ret)==typeof(Object[]) || typeof(ret)==typeof(String[]))
               return (ret as Object[])[Random.Range(0, (ret as Object[]).Length)];
           // else:
         return ret;
       }
   }
   return ""; 
 }

TestRun:

udueẽminanta diserimomichihe setenohiô tare hukoyoid lineninu kanis majokuja harosoung mianafuia taloomaziranuyieng livoeyu kovutaluisorora nalaiewaterenid sukuvasugaàong sosakatsuwu masuid raùfukafaketoũ hinakiisunuia yukis yoorimaung furueng atahofurui rusutekueng eyotemimimumaka soyumetamuress sonueng youheshifoē haóketehakimamuung batirasikakuno pohaxopenokoȋno kuûyanopi memu zokahaĭpeid yabiss rose ȗhoaneke okoseli ûtopashieng re°kesemaing îkeke etesozira c$$anonymous$$unimeniss ēayayuyesohake ekafashimanikefis jemuĕzumimawaki kiwaung iȋong tosagemeeng meĩ jihatsueng ganouc$$anonymous$$ uzetsudihuūsoyoss iba xoyujuro ûseid teronjeshino émoung teshipa gukesa ĕheuung tahofo raòid árajoll îkeyunuilixato dehukowehirilitoong meronarureung emukateô yuhumikuraweus totaifuwabióro hifiyatayame ngozexurahate chichiarakaia yaoòoung suȗtutemumezike kisarireyadiû hohaíriyaxuâia ranàrinnjaȇ sakayuyoeng imirekumamipitsuss meekuoèhageia yuesaia sefumakayosayoyaeng zewafuna wayasuhonmuōke erumito zechijill natakeá sali nabi tsuc$$anonymous$$ ru$$anonymous$$îùneuname sukaraong koãyaki jimehell jamavonehehisuri tiaia ifu mariid miutereing

Comment

People who like this

0 Show 3 · 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 Kasigawa · Jan 07, 2014 at 03:41 PM 0
Share

@HappyMoo This is perfect. A million thank yous! I never though the random pick could have been a function. I also see that I lack syntax grammar. Like how I used char instead of String. I also see that the probability of chance became a big part of the code as well. It's definitely done completely different then what I have imagined. I'm going to have to dissect this code to fully understand what is happening. Again thank you.

@Benproductions I acknowledge on my post I should have stated my bugs clearly, but the bugs I got didn't make any sense (missing semicolons on statements with semicolons; invalid switch tokens; and other bugs I didn't understand). I was also uncertain on how to write the code. I'm not much a coder anymore, this stuff is really hard for me now. My apologies.

avatar image HappyMoo Kasigawa · Jan 07, 2014 at 03:47 PM 0
Share

Thank you, doing this was fun. Please don't post comments as answers. "Answers" are supposed to answer the original question - have a look at the tutorial video linked in the right bar. I'm turning your answer into a comment.

avatar image HappyMoo · Jan 09, 2014 at 06:21 PM 0
Share

Please accept the answer

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

Multiple Cars not working 1 Answer

How do I make a respawn system? 1 Answer

Error in distance coding 1 Answer

Need Help Understanding Script! (Javascript) 1 Answer

"Expecting ), found ';'" and "';' expected. Insert a semicolon at the end" 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