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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Fullscreen checkbox always starts checked even when the game starts in windowed mode. 0 Answers

Remember Toggle Button State When Scene Return 0 Answers

Problems with scripting and invokeRepeating 1 Answer

Displaying C# Int Value to GUI 2 Answers

Toggle Button For Timed Device When Timer Runs Out 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges