• 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
0
Question by JohnArthur · Jun 26, 2014 at 11:40 AM · c#dialogue

Auto-type text does not change to the next sentence

 using UnityEngine;
 using System.Collections;
 
 
 //How to Use
 //Add this script to GUITEXT
 
 public class autoType : MonoBehaviour {
 
     //speed
     public float letterPause = 0.1f;
     //public AudioClip sound;
 
     //declare stanzas
     string stanza,stanza2,stanza3,stanza4;
     int where;
     
     // Use this for initialization
     void Start () {
         //put sentences here
         stanza = "THIS IS IT";
         stanza2 = "new Message";
         stanza3 = "hello World";
         stanza4 = "next stanza";
     }
 
     void Update()
     {
         //start auto type
         SetText(stanza);
         //condition to change
         if(where == 1)
         {
             stanza = "";
             stanza = stanza2;
         }
         if(where == 2)
         {
             stanza = "";
             stanza = stanza3;
         }
         if(where == 3)
         {
             stanza = "";
             stanza = stanza4;
         }
 
     }
     
     IEnumerator TypeText () {
         foreach (char letter in stanza.ToCharArray()) {
             guiText.text += letter;
             yield return 0;
             yield return new WaitForSeconds (letterPause);
         }
         //new stanza condition
         where++;
         Debug.Log (where);
     }
     void SetText(string text)
     {
         //StopCoroutine("TypeText");
         stanza = text;
         guiText.text = "";
         StartCoroutine("TypeText");
     }
 }

As the title says, the text does not change to the next stanza.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by gjf · Jun 26, 2014 at 02:26 PM

this will work, but it's not very friendly with the garbage collector - string concatenation, foreach, etc. but i'm guessing that getting it working is the most important to you right now ;)

 using UnityEngine;
 using System.Collections;

  public class AutoType : MonoBehaviour
 {
     public float LetterPause = 0.1f;
 
     private bool _textFinished;
     private int _textIndex;
     private string[] _stanzas;
 
     public void Start()
     {
         _stanzas = new []{ "THIS IS IT", "new Message", "hello world", "next stanza", "you're welcome!" };
 
         _textFinished = true;
         _textIndex = -1;
     }
 
     public void Update()
     {
         if (_textFinished)
         {
             if (_textIndex++ >= (_stanzas.Length - 1))
             {
                 _textIndex = 0;
             }
 
             SetText(_stanzas[_textIndex]);
         }
     }
 
     private IEnumerator TypeText(float waitTime, string text)
     {
         _textFinished = false;
 
         guiText.text = "";
 
         foreach (var letter in text)
         {
             guiText.text += letter;
 
             yield return new WaitForSeconds (waitTime);
         }
 
         _textFinished = true;
     }
 
     private void SetText(string text)
     {
         StartCoroutine(TypeText(LetterPause, text));
     }
 
     public void OnGUI()
     {
         GUI.Label(new Rect(10, 10, 150, 100), "Text:" + guiText.text);
     }
 }
Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

C# Transform Issues 1 Answer

C# GUI.DrawTexture Tooltip 0 Answers

How do i Instantiate a prefab with specific assests included 3 Answers

C# Child GameObject Disabled Script SetActive(false) 1 Answer

C# Twitchy Camera Movement 1 Answer

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