Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Apr 01 at 12:33 AM by PresidentTree for the following reason:

The issue is not what I thought it was.

avatar image
0
Question by PresidentTree · Mar 29 at 12:37 AM · scripting problemtextcoroutinedialoguecoroutine errors

Why is my coroutine skipping a method?

In my game, I'm making a dialogue system that displays text every time a certain button is pressed, but it doesn't work for one of the method calls. Two methods, ReadLine with three arguments (method RL3) and ReadLine with two arguments (method RL2), are available to display text, which is typed out like a typewriter. Everything works fine, but the first time method RL2 is called after method RL3, the text does not appear as though it was typed by a typewriter. When method RL2 is called before method RL3, the text appears correctly. If both methods are called separately, the text appears correctly. The methods are pretty much the same except for where the text is going and how many arguments they have. What am I missing here?

Dialogue Script:

 public class DialogueScript : MonoBehaviour
 {
     private Image dialogueBox;
     private TextMeshProUGUI nameBox;
     private TextMeshProUGUI textBox;
     private float textSpeed = 0.05f;
     private int textIndex;
 
     //Reference to dialogue box:
     void Start()
     {
         dialogueBox = GameObject.Find("Overlay").transform.GetChild(5).GetComponent<Image>();
         nameBox = dialogueBox.transform.GetChild(0).GetComponent<TextMeshProUGUI>();
         textBox = dialogueBox.transform.GetChild(1).GetComponent<TextMeshProUGUI>();
     }
 
     //Play dialogue:
     void Update()
     {
         OpenDialogueBox();
         ReadLine("Johnny", "I have a question!", 1);
         ReadLine("Answer the question?", 2);
         ReadLine("Are you sure?", 3);
         ReadLine("Roderick", "Ask away.", 4);
         CloseDialogueBox(5);
     }
 
     //Open dialogue box:
     void OpenDialogueBox()
     {
         if (!dialogueBox.gameObject.activeSelf && Input.GetKeyDown(KeyCode.T))
         {
             dialogueBox.gameObject.SetActive(true);
             nameBox.text = string.Empty;
             textBox.text = string.Empty;
             textIndex = 1;
         }
     }
 
     //Close dialogue box:
     void CloseDialogueBox(int closingIndex)
     {
         if (closingIndex == textIndex)
         {
             StopAllCoroutines();
             dialogueBox.gameObject.SetActive(false);
         }
     }
 
     //Types script to dialogue box:
     void ReadLine(string name, string text, int num)
     {
         if (Input.GetKeyDown(KeyCode.T) && num == textIndex)
         {
             if (textBox.text == text)
             {
                 textIndex++;
             }
             else if (textBox.text.Length < text.Length && textBox.text.Length > 0)
             {
                 StopAllCoroutines();
                 nameBox.text = name;
                 textBox.text = text;
             }
             else
             {
                 nameBox.text = string.Empty;
                 textBox.text = string.Empty;
                 StartCoroutine(TypeLine(name, text));
             }
         }
     }
 
     //Overload method for when there is no speaker:
     void ReadLine(string text, int num)
     {
         if (Input.GetKeyDown(KeyCode.T) && num == textIndex)
         {
             if (nameBox.text == text)
             {
                 textIndex++;
             }
             else if (nameBox.text.Length < text.Length && nameBox.text.Length > 0)
             {
                 StopAllCoroutines();
                 nameBox.text = text;
                 textBox.text = string.Empty;
             } else
             {
                 nameBox.text = string.Empty;
                 textBox.text = string.Empty;
                 StartCoroutine(TypeLine(text, null));
             }
         }
     }
 
     //Typewriter effect:
     IEnumerator TypeLine(string name, string text)
     {
         if (text == null)
         {
             foreach (char letter in name)
             {
                 nameBox.text += letter;
                 yield return new WaitForSeconds(textSpeed);
             }
         }
         else
         {
             nameBox.text = name;
             foreach (char letter in text)
             {
                 textBox.text += letter;
                 yield return new WaitForSeconds(textSpeed);
             }
         }
     }
 }
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

  • Sort: 
avatar image
0

Answer by rh_galaxy · Mar 29 at 01:59 AM

Don't know if there are more problems but one is that you can't check for T key down in ReadLine() because it will only be true for one frame, so it never runs all code inside the first if-statement.

Maybe you can just skip the T check, something like this

 //New state variables
 bool effectStarted = false;
 bool effectDone = false;
 
 //Play dialogue:
 void Update()
 {
     OpenDialogueBox();
     ReadLine("Johnny", "I have a question!", 1);
     ReadLine("Answer the question?", null, 2);
     ReadLine("Are you sure?", null, 3);
     ReadLine("Roderick", "Ask away.", 4);
     CloseDialogueBox(5);
 }
 
 //Types script to dialogue box:
 void ReadLine(string name, string text, int num)
 {
     if (num == textIndex)
     {
         if (!effectStarted)
         {
             effectStarted = true;
             effectDone = false;
             StartCoroutine(TypeLine(name, text));
         }
         if (effectDone)
         {
             textIndex++;
             effectStarted = false;
         }
     }
 }
 
 //Typewriter effect:
 IEnumerator TypeLine(string name, string text)
 {
     string s = text;
     if (text != null) nameBox.text = name;
     else s = name;
     foreach (char letter in s)
     {
         nameBox.text += letter;
         yield return new WaitForSeconds(textSpeed);
     }
     effectDone = true;
 }
Comment
Add comment · Show 1 · 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
avatar image PresidentTree · Mar 29 at 02:30 PM 0
Share

@rh_galaxy

Unless I implemented your code wrong, it does not work. In fact, if I copy what you did, the dialogue plays continuously without player input once the dialogue box opens. Did I do something wrong?

 public class DialogueScript : MonoBehaviour
 {
     private Image dialogueBox;
     private TextMeshProUGUI nameBox;
     private TextMeshProUGUI textBox;
     private float textSpeed = 0.05f;
     private int textIndex;
     bool effectStarted = false;
     bool effectDone = false;
 
     //Reference to dialogue box:
     void Start()
     {
         dialogueBox = GameObject.Find("Overlay").transform.GetChild(5).GetComponent<Image>();
         nameBox = dialogueBox.transform.GetChild(0).GetComponent<TextMeshProUGUI>();
         textBox = dialogueBox.transform.GetChild(1).GetComponent<TextMeshProUGUI>();
     }
 
     //Play dialogue:
     void Update()
     {
         OpenDialogueBox();
         ReadLine("Johnny", "I have a question!", 1);
         ReadLine("Answer the question?", null, 2);
         ReadLine("Are you sure?", null, 3);
         ReadLine("Roderick", "Ask away.", 4);
         CloseDialogueBox(5);
     }
 
     //Open dialogue box:
     void OpenDialogueBox()
     {
         if (!dialogueBox.gameObject.activeSelf && Input.GetKeyDown(KeyCode.T))
         {
             dialogueBox.gameObject.SetActive(true);
             nameBox.text = string.Empty;
             textBox.text = string.Empty;
             textIndex = 1;
         }
     }
 
     //Close dialogue box:
     void CloseDialogueBox(int closingIndex)
     {
         if (closingIndex == textIndex)
         {
             StopAllCoroutines();
             dialogueBox.gameObject.SetActive(false);
         }
     }
 
     //Types script to dialogue box:
     void ReadLine(string name, string text, int num)
     {
         if (num == textIndex)
         {
             if (!effectStarted)
             {
                 effectStarted = true;
                 effectDone = false;
                 StartCoroutine(TypeLine(name, text));
             }
             if (effectDone)
             {
                 textIndex++;
                 effectStarted = false;
             }
         }
     }
 
     //Typewriter effect:
     IEnumerator TypeLine(string name, string text)
     {
         string s = text;
         if (text != null) nameBox.text = name;
         else s = name;
         foreach (char letter in s)
         {
             nameBox.text += letter;
             yield return new WaitForSeconds(textSpeed);
         }
         effectDone = true;
     }
 }

Follow this Question

Answers Answers and Comments

287 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 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 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 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

Odd Coroutine behavior 0 Answers

Coroutine start in while 2 Answers

WaitForSeconds Not Working 4 Answers

DisplayText info and wait for animation ends to destroyobject ( time extra box) 0 Answers

I can't get Text Mesh to disappear in the scene / How do I make the Text Mesh disappear in my scene 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