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 /
  • Help Room /
This question was closed Apr 25, 2018 at 11:17 AM by tormentoarmagedoom for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Deeblock · Apr 23, 2018 at 08:54 AM · textcoroutinestopcoroutine errorsstopcoroutine

Coroutine does not stop?

I have called a coroutine to print out text given to it from another class. I want to disable the textfield after a set amount of seconds, and hence I used a coroutine. I do not want to use setActive() because my script is attached to the text field itself.

I'm calling the coroutine through a function that is called by another class, which checks a bool as to whether my coroutine is running or not. If it is, I want it stopped and started with the new parameter. However, when I run the script, the coroutine does not appear to be stopped (i.e. the text background still disappears after a few seconds on every coroutine call).

Is there any problem with the script? Thanks.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class UIPrinter : MonoBehaviour {
 
     public Text textField;
     public float textTime = 2;
 
     private bool printingText = false;
 
     private void Start()
     {
         textField = GetComponent<Text>();
     }
 
     public void UIPrint(string textToPrint)
     {
         if (printingText)
         {
             StopCoroutine("PrintText");
         }
         StartCoroutine(PrintText(textToPrint));
     }
 
     public IEnumerator PrintText(string textToPrint)
     {
         printingText = true;
         textField.text = textToPrint;
         transform.parent.gameObject.GetComponent<Image>().color = new Color32(0, 0, 0, 150);
         yield return new WaitForSecondsRealtime(textTime);
         transform.parent.gameObject.GetComponent<Image>().color = new Color32(0, 0, 0, 0);
         textField.text = "";
         printingText = false;
     }
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
1
Best Answer

Answer by Hellium · Apr 23, 2018 at 09:18 AM

The documentation says:

Note: Do not mix the three arguments. If a string is used as the argument in StartCoroutine, use the string in StopCoroutine. Similarly, use the IEnumerator in both StartCoroutine and StopCoroutine. Finally, use StopCoroutine with the Coroutine used for creation

 public class UIPrinter : MonoBehaviour {
  
      public Text textField;
      public float textTime = 2;
  
      private IEnumerator coroutine;
  
      private void Start()
      {
          textField = GetComponent<Text>();
      }
  
      public void UIPrint(string textToPrint)
      {
          if (coroutine != null)
          {
              StopCoroutine(coroutine);
          }
          coroutine = PrintText(textToPrint);
          StartCoroutine( coroutine );
      }
  
      public IEnumerator PrintText(string textToPrint)
      {
          textField.text = textToPrint;
          transform.parent.gameObject.GetComponent<Image>().color = new Color32(0, 0, 0, 150);
          yield return new WaitForSecondsRealtime(textTime);
          transform.parent.gameObject.GetComponent<Image>().color = new Color32(0, 0, 0, 0);
          textField.text = "";
          coroutine = null;
      }
 }

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 Deeblock · Apr 23, 2018 at 09:26 AM 0
Share

How do I assign the PrintText IEnumerator to the "coroutine" variable?

avatar image Hellium Deeblock · Apr 23, 2018 at 09:48 AM 0
Share

As indicated in my code:

 coroutine = PrintText(textToPrint)
avatar image Deeblock Hellium · Apr 23, 2018 at 10:02 AM 0
Share

Ah woops. Didn't catch that. Thanks for your help!

Follow this Question

Answers Answers and Comments

144 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

Related Questions

Coroutine not stopping 0 Answers

After stop coroutine it resumes its task when I call start coroutine 1 Answer

Can I check if this.gameObject was destroyed? 5 Answers

Need help using coroutines 1 Answer

IEnumerator instead of LateUpdate in unity 1 Answer


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