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
0
Question by laurienash · Jun 01, 2015 at 10:37 PM · coroutinepausebasic

Pause a coroutine not working

Hi - I'm struggling to get a coroutine working correctly.

Whilst a player is inside a collider, I set a coroutine to active. However, if the player is set to inactive whilst inside the collider, I want this coroutine to pause where it is in the sequence, and once this player is set to active again, I want the coroutine to resume where it is in the sequence.

Does anyone know what I'm doing wrong?

 #pragma strict
 
 var player : GameObject = null;
 var cameraPortacabin : GameObject;
 var text44 : GameObject;
 var text45 : GameObject;
 var text46 : GameObject;
 var screen1 : GameObject;
 
 var lastKnownState : boolean = true; // set default to false if camera starts disabled
 
 function Start () {
 
 text44.SetActive(false);
 text45.SetActive(false);
 text46.SetActive(false);
 screen1.SetActive(false);
 
 }
 
 function Update ()
  {
     if (cameraPortacabin.active != lastKnownState)
     {
        lastKnownState = cameraPortacabin.active;
        
        if (lastKnownState) // same as if (lastKnownState == true)
           portacabinSequence();
     }
     
     if (cameraPortacabin.active == false || player.active == false) {
     
     StopCoroutine("portacabinSequence");
     text46.SetActive(false);
 screen1.SetActive(false);
  }
  }
 
 function portacabinSequence () {
 
 
 screen1.SetActive(true);
 
 text44.SetActive(true);
 
 yield WaitForSeconds (9.5);
 
 text44.SetActive(false);
 text45.SetActive(true);
 
 yield WaitForSeconds (11);
 
 text45.SetActive(false);
 text46.SetActive(true);
 
 }

Best, Laurien

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

· Add your reply
  • Sort: 
avatar image
0

Answer by redeemer · Jun 01, 2015 at 10:51 PM

Hi,

If you don't have any other coroutines you need to keep active, you could try with StopAllCoroutines.

http://docs.unity3d.com/ScriptReference/MonoBehaviour.StopAllCoroutines.html

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 laurienash · Jun 02, 2015 at 12:03 AM 0
Share

But I want to pause it rather than stopping it altogether - or have I understood that wrong?

Once the player is active again, I'd like the coroutine to start from the point where it was paused until reaching the end of the coroutine.

avatar image redeemer · Jun 02, 2015 at 01:36 AM 0
Share

Well, maybe I'm wrong but I think you didn't undertand how a coroutine behaves. Your coroutine would be trigger each time last$$anonymous$$nownState equals true. Your coroutine does not have a loop, so it would be executed only once (each time is called, that mean if you called it 20 times it would be executed 20 times, one per call) with a total execution time of 20.f secs. It's possible that when you try to Stop your coroutine when cameraPortacabin or player are not active, your coroutine had already finished or maybe it was not even started in the first place. If you want to implement a "Pause" system, this is not the correct way. If this is the case, ther're other answers where this topic had already been discussed. As an example, this link could be of interest, it's a free example available at the Unity Store : https://www.assetstore.unity3d.com/en/#!/content/1020

Is this other answer you have multiple possible solutions for pausing your game :

http://answers.unity3d.com/questions/7544/how-do-i-pause-my-game.html

If your goal is not to make a pause system I'm sorry in advance and could you please be more specific about what is you want to accomplish?

avatar image laurienash · Jun 02, 2015 at 08:10 PM 0
Share

Sorry for not being clear!

Yes - the coroutine is only executed once (each time the player enters a trigger).

So essentially I need:

if (player.active == false) {

//remember what point in the coroutine this is and stop coroutine

if (player.active == true) {

Start coroutine at the last point the coroutine was in the sequence until reaching the end of the loop.

But I can't work out how to do this? (I haven't formatted the code correctly as I'm a bit embarrassed as it's not really code - I just can't work out how to go about this)

avatar image redeemer · Jun 02, 2015 at 10:00 PM 0
Share

Ok, but when you say "Start coroutine at the last point the coroutine was in the sequence until reaching the end of the loop." to which loop are you refering, because in your coroutine there is no loop, and I don't think is possible to stop at a point in the code to jump back at it (goto died a long time ago XD)...

In order for a coroutine to have a loop, you must declare a for or while loop such as :

     function portacabinSequence () {
      
        while(player.active) {
     
           screen1.SetActive(true);
      
           text44.SetActive(true);
      
           yield WaitForSeconds (9.5);
      
           text44.SetActive(false);
           text45.SetActive(true);
      
           yield WaitForSeconds (11);
       
           text45.SetActive(false);
           text46.SetActive(true);
     
        }
     
     }

For what you want to implement I would recommend to create different possible states in which the player can be left and the logics for each one of them.

I also think that even if you call StopCoroutine("CoroutineName") or StopAllCoroutines, it will execute the cicle it's in until the end and then it'll exit, so you can't stop it at a specific point.

Without further explanation of your code, scene or goal I really can't say anything else, sorry...

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

21 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

Related Questions

Coroutines, WaitForSeconds with function Update? 3 Answers

How to pause and unpause my game - Input.GetAxis doesn't work when Time.timeScale is 0 3 Answers

How to disable game elements for a fixed duration then re-enable them automatically, even if game is reopened 1 Answer

Pause and resume coroutine 0 Answers

Is there a way to make WaitForSeconds ignore time.timescale? 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