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 /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Feb 02, 2016 at 03:34 PM · c#invokerepeatingtoggle buttonrepeating

Invoke Repeating Problem

I'm new to the whole invoke repeating part of code so naturally having issues with this. I'm trying to get it so that if I have my repeat toggle "ON" then after a specified time frame the message will be repeated. But I keep getting this:

Error In Console

 using UnityEngine;
 using System.Collections;
 using System.Text.RegularExpressions;
 using UnityEngine.UI;
 
 public class MorseCodePlayer : MonoBehaviour {
 
     public bool iSOn = false;
 
     public InputField inputFieldMCode;
     public    Text morseCodeDisplayText;
 
     public AudioClip dotSound;
     public AudioClip dashSound;
     public float spaceDelay;
     public float letterDelay;
     
     // International Morse Code Alphabet
     private string[] alphabet = 
     //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
      "...-", ".--", "-..-", "-.--", "--..",
     //0        1        2        3        4        
      "-----", ".----", "..---", "...--", "....-", 
     //5        6        7        8        9     
      ".....", "-....", "--...", "---..", "----."};
 
     // Use this for initialization
         void Start () 
     {
         //morseCodeDisplayText = GameObject.Find ("MorseCodeDisplayText").GetComponent<Text> ();
         inputFieldMCode = GameObject.Find ("InputField").GetComponent<InputField> ();
         
         // H   E  L    L   O       W  O    R   L   D
         //.... . .-.. .-.. ---    .-- --- .-. .-.. -..
                 //PlayMorseCodeMessage("Hello World.");
                 PlayMorseCodeMessage ("");
     }
 
         public void MorseCodeField(string inputFieldString){
                 morseCodeDisplayText.text = inputFieldString;
         }
 
         public void RepeatMessage (){
                 Debug.Log ("Checking if Bool is On");
                 if (iSOn = true) {
                         InvokeRepeating ("repeat", 15f, 1f);
                         Debug.Log ("I will Invoke Repeat");
                 }
                 Debug.Log ("Confirming Bool Is On!");
         }
 
         //Repeat Message Being Sent after "X" time has passed.
         void repeat (string message){
                 Debug.Log ("I Will Submit Text");
                 StartCoroutine("_PlayMorseCodeMessage", message); //I thought this would just restart the message send Coroutine?
                 submitText ();
                 Debug.Log ("I HAVE Submited Text");
         }
                 
 
         public void readButton (string c){
                 
                 inputFieldMCode.text += c;
 
         }
 
         string messageToSend;
         public void submitText(){
                 messageToSend = inputFieldMCode.text;
         }
 
     
     
         public void PlayMorseCodeMessage(string message)
     {
         StartCoroutine("_PlayMorseCodeMessage", message);
     }
     
         private IEnumerator _PlayMorseCodeMessage(string message)
     {
         // Remove all characters that are not supported by Morse code...
         Regex regex = new Regex("[^A-z0-9 ]");
         message = regex.Replace(message.ToUpper(), "");
         
         // Convert the message into Morse code audio... 
         foreach(char letter in message)
         {
             if (letter == ' ')
                 yield return new WaitForSeconds(spaceDelay);
             else
             {
                 int index = letter - 'A';
                 if (index < 0)
                     index = letter - '0' + 26;
                 string letterCode = alphabet[index];
                 foreach(char bit in letterCode)
                 {
                     // Dot or Dash?
                     AudioClip       sound = dotSound;
                     if (bit == '-')    sound = dashSound;
                     
                     // Play the audio clip and wait for it to end before playing the next one.
                     GetComponent<AudioSource>().PlayOneShot(sound);
                     yield return new WaitForSeconds(sound.length + letterDelay);
                 }
             }
         }
     }
 }
 
screen-shot-2016-02-02-at-93104-am.png (44.2 kB)
Comment
Add comment · Show 1
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 YourFutureHero · Mar 17, 2016 at 05:12 AM 0
Share

You have 2 ways of going about it probably but... You currently have 2 ways of calling the Coroutine yet you call it only once and you aren't ever calling InvokeRepeating. There is a "life cycle" that takes place within a Unity script. You see when you create a new script it starts with a start and update function? Those are part of the life cycle or as Unity calls it, execution order (android mobile calls it life cycle), but I would recommend you check out this link...Execution Order. That document has more to it than I could explain but it's very helpful. Also, when you use InvokeRepeating, you are probably not wanting to repeatedly call a Coroutine as this can lead to unwanted behavior. How often do you want the message to repeat?

0 Replies

· Add your reply
  • Sort: 

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

75 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

Related Questions

I want to generate gun fire whenever i press Space button 1 Answer

Can't find error in code 1 Answer

Need help understanding toggle code. 0 Answers

Object moving chaotically using lerp and repeating. 1 Answer

What is the most accurate way to call a function based on time? 0 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