Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by bentance · Mar 21, 2019 at 10:53 AM · vrdialogue

Dialogue Text becomes gibberish if activate too quickly

I am trying to create a dialogue system where the dialogue will continue if the player press E. However, if the player press E too quickly, the dialogue text will become gibberish as shown in the screenshot. Please advise. Thanks

IEnumerator Type() { foreach (char letter in sentences[index].ToCharArray()) { displayText.text += letter; yield return new WaitForSeconds(typingspeed); }

 }

public void NextSentenceGuitar() { if (index < sentences.Length - 1) {

         index++;
         displayText.text = "";
         StartCoroutine(Type());

     }
     else
     {
         displayText.text = "";
         Canvas.SetActive(false);
     }



 }








alt text

error.jpg (74.9 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 Tsaras · Mar 21, 2019 at 12:13 PM 0
Share

Could it be activating before the arrays are populated? Or reading from uninitialized memory?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by mchts · Mar 21, 2019 at 12:09 PM

I dont know what else happens when pressed on E (maybe problem lying under there) but i could suggest that to prevent user press E too early. Just define a bool e.g. typing to keep track of your typing activity. Set it false initially and when typing coroutine starts set it true (or when you need it).

 if(Input.GetKeyDown(KeyCode.E) && typing){
     //do your stuff
 }   
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
avatar image
1

Answer by timoffex · Mar 22, 2019 at 07:03 PM

This is happening because the previous Type() coroutine keeps going even if you start a new one. So if the user presses E twice in rapid succession, the first Type() method will start adding letters from sentences[0] and the second will start adding letters from sentences[1] simultaneously, causing the sentences to interleave with each other.

An appropriate solution is to stop the previous Type() coroutine before you start a new one, like so:

 Coroutine typeCoroutine = null;
 
 public void NextSentenceGuitar()
 {
     if (index < sentences.Length - 1)
     {
         index++;
         displayText.text = "";
         if (typeCoroutine != null)
             StopCoroutine(typeCoroutine);
         typeCoroutine = StartCoroutine(Type());
     }
     else
     {
         displayText.text = "";
         Canvas.SetActive(false);
     }
 }

 IEnumerator Type()
 {
     foreach (char letter in sentences[index].ToCharArray())
     {
         displayText.text += letter;
         yield return new WaitForSeconds(typingspeed);
     }

     typeCoroutine = null;
 }
Comment
Add comment · Show 4 · 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 bentance · Mar 23, 2019 at 10:59 PM 0
Share

Hi timoffex, thanks for spending the effort to put out the codes. I have copy pasted your code into Unity however it is still not working. =(

Coroutine typeCoroutine = null;

 IEnumerator Type()
 {
     foreach (char letter in gameSentences[gameIndex].ToCharArray())
     {
         GameText.text += letter;
         yield return new WaitForSeconds(GameTypingSpeed);
     }
     typeCoroutine = null;
 }

 public void GameStartSentence()
 {
     if (gameIndex < gameSentences.Length - 1)
     {
         gameIndex++;
         GameText.text = "";
         if (typeCoroutine != null)
             StopCoroutine(typeCoroutine);
         StartCoroutine(Type());
     }
     else
     {
         GameText.text = "";
         GameCanvas.SetActive(false);
     }
 }
avatar image timoffex bentance · Mar 24, 2019 at 01:24 AM 0
Share

In GameStartSentence(), you're forgetting to set typeCoroutine:

 typeCoroutine = StartCoroutine(Type());

avatar image bentance timoffex · Mar 24, 2019 at 07:20 PM 0
Share

Thanks timoffex, my codes are working now. I didnt get it to work in the first time but I changed it after I learn about the concept of typeCoroutine = null. Below is the code that worked for me. Thanks a lot man, youre a legend.

=D

Coroutine typeCoroutine = null; IEnumerator Type() {

     foreach (char letter in gameSentences[gameIndex].ToCharArray())
     {
         GameText.text += letter;
         yield return new WaitForSeconds(GameTypingSpeed);
         
         
     }
     typeCoroutine = null;
 }

Show more comments
avatar image
0

Answer by bentance · Mar 22, 2019 at 03:29 PM

Thanks mchts, I think you are right, the lines are executed if I hold down the E button for too long. I am not exactly sure what your solution meant but I will try to figure it now. Would appreciate if you can provide a bit more information.

Comment
Add comment · Show 3 · 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 mchts · Mar 22, 2019 at 08:49 PM 0
Share

I think i could help better if you provide some more detail about your code. Especially about what happens when you press $$anonymous$$ @timoffex's solution seems pretty reasonable too btw.

avatar image bentance mchts · Mar 24, 2019 at 07:23 PM 0
Share

Yeah, I just learnt the concept of typeCoroutine = null and applied it. $$anonymous$$y codes are working now. Thanks for pointing out the error in the first place mchts.

Have a good one =D

avatar image mchts bentance · Mar 26, 2019 at 08:04 AM 0
Share

You're welcome my friend!

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.

Follow this Question

Answers Answers and Comments

134 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

Related Questions

VR RPG like dialogue system 1 Answer

VR Steam VR Asset 1 Answer

Unity Native VR doesn't work!! 0 Answers

Unity Daydream Technical Preview lacks controller support and crashes using Google VR SDK 1 Answer

What are some of the possible reasons that disabling shadows decreases performance rather than enhancing it? 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