• 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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by brianchan661 · Aug 17, 2015 at 09:38 AM · stopcoroutine

start and stop a Coroutine in a non MonoBehaviour script

I have a non monoBehaviour script that want to call startCoroutine,
and I successfully did it using the ans here:
http://answers.unity3d.com/questions/161084/coroutine-without-monobehaviour.html

While I also want to stop the coroutine when receive player input, say call StopCoroutine(String) when player press spacebar.

I have tried to change the StartChildCoroutine's parameter as string (so that I can use the StopCoroutine, but startCoroutine failed.)

Can anyone suggest a solution to stop my coroutine?

 public class NPCSpeeching : MonoBehaviour {
      private List<SpeechContext> allSpeech=new List<SpeechContext>();
      void Start () {
         allSpeech[0].doSpeechOperation();
      }

     public void StartChildCoroutine(IEnumerator coroutineMethod)
     {
         StartCoroutine(coroutineMethod);
     }


      public class SpeechContext{
         SpeechStrategy strategy;
 
         public SpeechContext(SpeechStrategy strategy){
             this.strategy=strategy;
         }
 
         public string getSentence (){
             return strategy.getSentence ();
         }
 
         public void doSpeechOperation(){
             strategy.SpeechAlgorithmInterface ();
         }
     }
 }


//The strategy interface

 public abstract class SpeechStrategy {
     
         public abstract void SpeechAlgorithmInterface();
     
         public abstract string getSentence();
     
     
     }

//A strategy

 public class NormalSpeechStrategy : SpeechStrategy {
     
         public MySentence mySentence;
     
         public NormalSpeechStrategy(MySentence mySentence,NPCSpeeching NPCspeeching,SpeechManager speechManager,Image speechUIImage,Text speechUiText):base(NPCspeeching,speechManager,speechUIImage,speechUiText){
     
             this.mySentence = mySentence;
         }
     
 
         public override void SpeechAlgorithmInterface(){
             speechManager.displaySpeechUI ();
             npcSpeeching.StartChildCoroutine (printOneString());       //the function can be print successfully. 
     
         }
     
         private IEnumerator printOneString(){
             string currentWord=null;
             foreach(char character in mySentence.sentence.ToCharArray()){
                 currentWord+=character;
                 speechUiText.text=currentWord;
                 yield return new WaitForSeconds (mySentence.speechSpeed);
             }
             speechManager.startShiftingNextIcon();
         }
     }



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

0 Replies

· Add your reply
  • Sort: 

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

25 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

Related Questions

Cancel WWW post operation 1 Answer

How to stop coroutine with parameters? 3 Answers

Is there any way to stop WaitForSeconds() ? 1 Answer

Brain exploding ... Problem with an internal Coroutine manager 3 Answers

Is there any performance differences between Coroutine methods ? 3 Answers

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