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:
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);
}
}
}
}
}
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?