Question by
bundoorahul · Jun 02, 2018 at 02:56 AM ·
coroutinepause game
How to pause a coroutine when an image is clicked from another script?
Hello, so i have used a coroutine method as a timer for game in my timer script and i have another script called GameManager which contains a method onMouseDown() which needs to pause the coroutine in the timer script when a image on the scene is clicked. How can this be done? Any help provided will be appreciated. Thanks
My timerscript:
public void Start() {
Time.timeScale = 1;
StartCoroutine("GameTimer");
//countdown.text = ("2:00");
//timeLeft--;
}
void Update() {
string minutes = ((int)timeLeft / 60).ToString (); //calculating minutes
int seconds = (timeLeft % 60); //calculating seconds in 2dp
if (seconds < 10) {
countdown.text = (minutes + ":0" + seconds.ToString()); //Showing the timer on the Canvas
} else {
countdown.text = (minutes + ":" + seconds.ToString()); //Showing the timer on the Canvas
}
if (timeLeft <= 0) {
StopCoroutine ("GameTimer"); //stop the second timer
SceneManager.LoadScene ("Main");
}
}
IEnumerator GameTimer()
{
while (true) {
yield return new WaitForSeconds (1);
timeLeft--;
}
}
My GameManager script:
void OnMouseDown(){
if (but.GetComponent<SpriteRenderer> ().sprite == OnSprite) {
but.GetComponent<SpriteRenderer> ().sprite = OffSprite;
pause = true;
pauseMenu.GetComponent<Transform> ().position = new Vector2 (0, -1);
}
else {
but.GetComponent<SpriteRenderer> ().sprite = OnSprite;
pause = false;
pauseMenu.GetComponent<Transform> ().position = new Vector2 (-450, 1);
}
}
Comment
Your answer
Follow this Question
Related Questions
how to Run a Coroutine even game is paused? 1 Answer
How to implement pausing of WaitForSeconds based coroutine? 1 Answer
Getting variable from script while keeping other code from it 1 Answer
How to deactivate a dropdown list when changing UI tabs 0 Answers
Coroutine doesn't continue after yield another coroutine 0 Answers