Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to call an array containing a string of text in order rather than randomly

Discussion in 'Scripting' started by uqmyou13, Apr 20, 2020.

  1. uqmyou13

    uqmyou13

    Joined:
    Mar 28, 2020
    Posts:
    5
    I watched a youtube tutorial on how to create a typewriter effect when displaying text. This all works however I would like the text from the array to appear in order rather than in a random order. I understand I need to set an index of some sort and add one each time, however, I'm unsure how to go about changing the script. Would anyone be able to help me? The video I watched was this if that helps give more background!



    Code (CSharp):
    1. private Text messageText;
    2.     private TextWriter.TextWriterSingle textWriterSingle;
    3.     private AudioSource talkingAudioSource;
    4.  
    5.     private void Awake() {
    6.         messageText = transform.Find("message").Find("messageText").GetComponent<Text>();
    7.         talkingAudioSource = transform.Find("talkingSound").GetComponent<AudioSource>();
    8.  
    9.         transform.Find("message").GetComponent<Button_UI>().ClickFunc = () => {
    10.             if (textWriterSingle != null && textWriterSingle.IsActive()) {
    11.                 // Currently active TextWriter
    12.                 textWriterSingle.WriteAllAndDestroy();
    13.             } else {
    14.                 string[] messageArray = new string[] {
    15.                     "Hi there! My name is Barry and I need your help!",
    16.                     "How are you?",
    17.                     "What's up?",
    18.                     "Help me make this game please!",
    19.                 };
    20.  
    21.                 string message = messageArray[Random.Range(0, messageArray.Length)];
    22.                 StartTalkingSound();
    23.                 textWriterSingle = TextWriter.AddWriter_Static(messageText, message, .05f, true, true, StopTalkingSound);
    24.             }
    25.          };
    26.     }
    27.  
    28.     private void StartTalkingSound() {
    29.         talkingAudioSource.Play();
    30.     }
    31.  
    32.     private void StopTalkingSound() {
    33.         talkingAudioSource.Stop();
    34.     }
    35.  
    Thanks in advance for your help :)
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    First, you want to make your message array global and add a global index value that tracks it, otherwise they'd be re-declared every time the anonymous method runs.
    Code (CSharp):
    1. public class SomeClass {
    2.    string[] messageArray = new string[] {
    3.       "Hi there! My name is Barry and I need your help!",
    4.       "How are you?",
    5.       "What's up?",
    6.       "Help me make this game please!",
    7.    };
    8.  
    9.    int msgIndex= 0;
    10.  
    11.    //etc...
    12. }
    Then replace this:
    Code (CSharp):
    1. string message = messageArray[Random.Range(0, messageArray.Length)];
    2. StartTalkingSound();
    3. textWriterSingle = TextWriter.AddWriter_Static(messageText, message, .05f, true, true, StopTalkingSound);
    With this:
    Code (CSharp):
    1. if(msgIndex < messageArray.Length - 1) {
    2.    string message = messageArray[msgIndex];
    3.    msgIndex++;
    4.  
    5.    StartTalkingSound();
    6.    textWriterSingle = TextWriter.AddWriter_Static(messageText, message, .05f, true, true, StopTalkingSound);
    7. }
     
  3. uqmyou13

    uqmyou13

    Joined:
    Mar 28, 2020
    Posts:
    5
    Thank you so much for your reply - sorry if this is quite simple (I'm still learning). I'm not sure if I have done it right though as the new if statement added has some errors - it now says that the msgIndex and messageArray do not exist in the current context.

    This is what I've changed the script to now.

    Code (CSharp):
    1.  private Text messageText;
    2.     private TextWriter.TextWriterSingle textWriterSingle;
    3.     private AudioSource talkingAudioSource;
    4.  
    5.     public class SomeClass {
    6.         string[] messageArray = new string[] {
    7.             "Hi there! My name is Barry and I need your help!",
    8.             "How are you?",
    9.             "what's up?",
    10.             "Help me make this game please!",
    11.         };
    12.  
    13.         int msgIndex = 0;
    14.     }
    15.  
    16.  
    17.     private void Awake() {
    18.         messageText = transform.Find("message").Find("messageText").GetComponent<Text>();
    19.         talkingAudioSource = transform.Find("talkingSound").GetComponent<AudioSource>();
    20.  
    21.         transform.Find("message").GetComponent<Button_UI>().ClickFunc = () => {
    22.             if (textWriterSingle != null && textWriterSingle.IsActive()) {
    23.                 // Currently active TextWriter
    24.                 textWriterSingle.WriteAllAndDestroy();
    25.             } else {
    26.                 if (msgIndex < messageArray.Length - 1)
    27.                 {
    28.                     string message = messageArray[msgIndex];
    29.                     msgIndex++;
    30.  
    31.                     StartTalkingSound();
    32.                     textWriterSingle = TextWriter.AddWriter_Static(messageText, message, .05f, true, true, StopTalkingSound);
    33.                 }
    34.             }
    35.          };
    36.     }
    37.  
    38.     private void StartTalkingSound() {
    39.         talkingAudioSource.Play();
    40.     }
    41.  
    42.     private void StopTalkingSound() {
    43.         talkingAudioSource.Stop();
    44.     }
    45.  
    46. }
     
    Last edited: Apr 21, 2020
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Ah, you don't actually need to add the "SomeClass" part - I just used that to emphasize that the array and index should be global variables (I.E: declared in the class-context, rather than in the method-context);

    Also, I just noticed that I made a mistake when checking the index value. It doesn't need to be compared with the array length - 1, just the array length itself should work without causing an IndexOutOfBoundsException:
    Code (CSharp):
    1.  private Text messageText;
    2.     private TextWriter.TextWriterSingle textWriterSingle;
    3.     private AudioSource talkingAudioSource;
    4.  
    5.     string[] messageArray = new string[] {
    6.         "Hi there! My name is Barry and I need your help!",
    7.         "How are you?",
    8.         "what's up?",
    9.         "Help me make this game please!",
    10.     };
    11.  
    12.     int msgIndex = 0;
    13.  
    14.     private void Awake() {
    15.         messageText = transform.Find("message").Find("messageText").GetComponent<Text>();
    16.         talkingAudioSource = transform.Find("talkingSound").GetComponent<AudioSource>();
    17.  
    18.         transform.Find("message").GetComponent<Button_UI>().ClickFunc = () => {
    19.             if (textWriterSingle != null && textWriterSingle.IsActive()) {
    20.                 // Currently active TextWriter
    21.                 textWriterSingle.WriteAllAndDestroy();
    22.             } else {
    23.                 if (msgIndex < messageArray.Length)
    24.                 {
    25.                     string message = messageArray[msgIndex++];
    26.  
    27.                     StartTalkingSound();
    28.                     textWriterSingle = TextWriter.AddWriter_Static(messageText, message, .05f, true, true, StopTalkingSound);
    29.                 }
    30.             }
    31.          };
    32.     }
    33.  
    34.     private void StartTalkingSound() {
    35.         talkingAudioSource.Play();
    36.     }
    37.  
    38.     private void StopTalkingSound() {
    39.         talkingAudioSource.Stop();
    40.     }
    41. }
     
    uqmyou13 likes this.
  5. uqmyou13

    uqmyou13

    Joined:
    Mar 28, 2020
    Posts:
    5
    This is what I have changed the script
    Thanks that worked perfectly! I appreciate your help and patience :)
     
  6. Leonetienne500

    Leonetienne500

    Joined:
    Dec 5, 2016
    Posts:
    130
    Code (CSharp):
    1. string randomString = stringPool[Random.Range(0, stringPool.Length - 1)];