Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question How to Reset the Time Specified in coroutine

Discussion in 'Scripting' started by lsk111919, Sep 4, 2020.

  1. lsk111919

    lsk111919

    Joined:
    Jul 8, 2020
    Posts:
    19
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine;
    5. using UnityEngine.Video;
    6. using UnityEngine.Timeline;
    7.  
    8. public class EKKOVideo : MonoBehaviour
    9. {
    10.     public VideoPlayer mVideoPlayer = null;
    11.  
    12.     public void Start()
    13.     {
    14.         mVideoPlayer.loopPointReached += FinishedVideo;
    15.     }
    16.  
    17.     public void FinishedVideo(VideoPlayer player)
    18.     {
    19.         StartCoroutine(StartLoadScene());
    20.     }
    21.  
    22.     IEnumerator StartLoadScene()
    23.     {
    24.        
    25.         yield return new WaitForSeconds(300f);
    26.         SceneManager.LoadScene("MainScenes");
    27.     }
    28. }

    I made the screen change 5 minutes after the video is over, but I would like to reset the time if I click the video again within 5 minutes. What should I do?
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    If you really just want to reset the time, stop the old coroutine and start a new one. For that you will have to save the IEnumerator of the coroutine to your class. When you detect a click on the video, use that IEnumerator to stop the coroutine, then run start a new one and overwrite the old saved IEnumerator.
    The documentation of StopCoroutine offers a small example for how to stop a coroutine:
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.StopCoroutine.html
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    You can also save the coroutine object that is returned by the StartCoroutine call itself and stop that.
     
    eses likes this.