Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by eradiuslore · Mar 16, 2012 at 01:15 PM · textyieldwaitforsecondsscrollstartcoroutine

Scrolling Text StartCoroutine not working C#

Hey, im in the process of implementing scrolling text which makes use of a coroutine to pause between each letter thats added to a string, that string is then assigned to a GUIText. what should happen is the letters in the text appear 1 at a time with a delay between each. what is actually happening is similar except without the delay.

i know that this question is obviously asked a fair amount, but the answers i have found seem to be what i already have in place so my only though is maybe something else in my code is wrong, if anybody can point out the problem i would most appreciate it :)

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class scrolltext : MonoBehaviour {
     
     public string m_TextBody="";
     public float m_PauseTime;
     private char[] m_LetterArray;
     private string m_TempBuffer="";
     private GUIText m_GUI;
     private int m_Counter;
     
     public void Start () {
         m_GUI = gameObject.guiText;
         m_LetterArray = m_TextBody.ToCharArray();
         m_TempBuffer = m_LetterArray[0].ToString();
         m_Counter++;
     }
     
     public void OnGUI(){
         m_GUI.text = m_TempBuffer;
     }
     
     public void Update(){
         nextLetter();
     }
     
     private void nextLetter(){
         if(m_Counter < m_LetterArray.Length){
             m_TempBuffer += m_LetterArray[m_Counter];
             StartCoroutine(WaitNumberOfSeconds(m_PauseTime));
             m_Counter++;
         }
     }
     
     private IEnumerator WaitNumberOfSeconds(float _val){
         yield return new WaitForSeconds(_val);
     }
 }
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

2 Replies

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

Answer by aldonaletto · Mar 16, 2012 at 04:09 PM

I would change the logic a little: start with a blank string and m_Counter = 0, and place all the donkey work in the nextLetter function, which must be a coroutine.
When you start a coroutine, an instance of it is created and starts running; if you start this coroutine again before the previous one has finished, you get new instances running independently. To avoid this, you should use a boolean flag to tell when you can call nextLetter to add a new character - set it to false at the coroutine start, and set it to true when it finishes: ...

 public void Start () {
    m_GUI = gameObject.guiText;
    m_LetterArray = m_TextBody.ToCharArray();
    m_TempBuffer = ""; // start with blank string...
    m_Counter = 0;     // thus no characters added yet
 }

 bool canAdd = true; // only add new characters when this flag is true
 
 public void Update(){
   if (canAdd){ // if can add new character...
     StartCoroutine(nextLetter()); // add it!
   }
 }

 private IEnumerator nextLetter(){
   canAdd = false; // disable nextLetter calls until the routine finishes
   if (m_Counter < m_LetterArray.Length){        // if there's any character to add...
     m_TempBuffer += m_LetterArray[m_Counter++]; // add it and advance counter...
     m_GUI.text = m_TempBuffer;                  // update the GUIText...
     yield return new WaitNumberOfSeconds(m_PauseTime); // and wait m_PauseTime...
     canAdd = true;   // re-enable nextLetter calls
   }
 }

Notice that when nextLetter is called and there are no more characters to add, canAdd never gets true again, what avoids further calls to nextLetter - an extra benefit!

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 eradiuslore · Mar 16, 2012 at 04:20 PM 0
Share

thank you very much, few mistakes but you prob just copied and pasted my code :P very helpful and full of awesome-ness

avatar image
1

Answer by syclamoth · Mar 16, 2012 at 02:20 PM

There are two problems here- the first is that the function 'nextLetter' needs to also be a coroutine if you want it to pause in the middle. However, because you don't want to start a new coroutine every frame (only when the counter should be incremented), the solution is a bit more subtle than that.

There are two possible ways to go here- you can move the

 m_Counter++;

line down into the WaitForNumberOfSeconds coroutine- that would make sure it waits.

However, that might come off as bad form (because it doesn't reflect the name of the function very well). If doing it that way disgusts you too much, you could also split that off into two functions-

 IEnumerator WaitAndIncrement(float waitTime)
 {
     yield return new StartCoroutine(WaitNumberOfSeconds(waitTime));
     m_Counter++;
 }

Then in 'nextLetter', just use

 StartCoroutine(WaitAndIncrement(m_PauseTime));
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 eradiuslore · Mar 16, 2012 at 04:21 PM 0
Share

didn't totally work correctly but still helpful in getting my head around the coroutine so thank you :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Fire rate for gun script, c# (not u/s) 1 Answer

Another question about yield, WaitForSeconds in c# 2 Answers

Yield and WaitForSeconds not stopping at correct points? 1 Answer

C# WaitForSeconds doesn't seem to work ?? 2 Answers

WaitForSeconds problem with Unity Pro 3 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