- Home /
How do I pause everything in the scene until the animation countdown prefabs done counting down in the scene
I have some countdown animations prefabs . I can only play the countdown animations in the scene if I drag the prefab in the scene and click on play . What I am trying to is play all 10 or 5 animation countdown prefab. But I want the scene to pause everything including the player and music until the countdown animations are done counting down in the scene. If have anymore questions just ask me.
Answer by Cence99 · Aug 07, 2016 at 10:23 PM
From a coder's perspective I would disable all scripts (or all scripts that move things or do the gameplay), freeze rigidbodies, disable audio components etc. until the countdown finished. Then just enable them again.
I can disable the scripts. How will do them all at the same time ?
Do I need to code ? I just want the gameobject to be pause .
Yes you need to code as this is game logic you'd like to change
Give an example please of a code . Like how I should come at it.
Answer by Anhvuive · Aug 08, 2016 at 02:10 PM
You can set timescale = 0 And Run static StartCourotine on static class to countdown and reset timescale = 1 after finish
code demo: using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Test : MonoBehaviour
{
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Time.timeScale = 0;
StartCoroutine(ExtentCountDown.CountDownWithoutTimeScale(1, 10, GetComponent<Text>()));
}
}
}
public static class ExtentCountDown
{
public static IEnumerator CountDownWithoutTimeScale(int interval, int duration, Text txtView)
{
while (true)
{
float pauseEndTime = Time.realtimeSinceStartup + duration;
while (Time.realtimeSinceStartup < pauseEndTime)
{
Debug.Log(Time.realtimeSinceStartup + " : " + pauseEndTime);
txtView.text = (pauseEndTime - Time.realtimeSinceStartup).ToString("00");
yield return 0;
}
Time.timeScale = 1;
break;
}
}
}
Answer by PhantomSarcasm · Aug 08, 2016 at 02:52 PM
there is a way coded. You can spend the whole scene, this stops stop or you can make it that way ma slow pace.
if you use:
Time.timeScale=0; stop
Time.timeScale=1; play
Time.timeScale=0.5f; half speed
remember that this will stop all animations and corutinas but will not stop the update or fixedupdate.
Here is my script for the countdown. Where would I be add that code ?
using UnityEngine;
using System.Collections;
public class CountDown$$anonymous$$anager : $$anonymous$$onoBehaviour
{
//Store the whole countdown objects
public GameObject[] countDownObjects;
//Define the countdown starting number, it should between 0-9!
public int startNumber = 3;
//Define the countdown ending number, it should between 0-9!
public int endNumber = 0;
//Define whether display the "GO!" logo at the end
public bool displayGoAtTheEnd = true;
//Define when will start to countdown
public float startAfterSeconds = 0.0f;
//Time counter
private float myTime = 0.0f;
//Cache the trasform of object
private Transform myTrans;
//Define the direction , 1 means from less to more, -1 means from more to less.
private int direction = -1;
//Define acctually countdown objects
private GameObject[] countdown;
//Define the trigger of spawn countdown object
private bool canSpawn = false;
private int number;
// Use this for initialization
void Start ()
{
//Cache the transform
myTrans = transform;
//If the number out of range, tell the user.
if(startNumber>9 || startNumber<0 || endNumber >9 || endNumber <0){
print("StartNumber and EndNumber should between 0-9! Please reinput the numbers.");
}
//$$anonymous$$ake sure the start and end frame is between 0-9 integer
startNumber = $$anonymous$$athf.Clamp(startNumber,0,9);
endNumber = $$anonymous$$athf.Clamp(endNumber,0,9);
//Define the correct direction
if( endNumber > startNumber ){
direction = 1;
}
number = startNumber;
//Whether need to show "GO!" at last
if(displayGoAtTheEnd){
//Define the acctually countdown objects array
countdown = new GameObject[$$anonymous$$athf.Abs(endNumber - startNumber) + 2];
for(int i=0 ; i<countdown.Length-1; i++){
countdown[i] = countDownObjects[number];
number += direction;
}
//If need to show "GO!" at last , add the "GO!" countdown object to the end of array.
countdown[ countdown.Length -1 ] = countDownObjects[10];
} else {
//Define the acctually countdown objects array(do not need to show "GO!")
countdown = new GameObject[$$anonymous$$athf.Abs(endNumber - startNumber) + 1];
for(int i=0 ; i<countdown.Length; i++){
countdown[i] = countDownObjects[number];
number += direction;
}
}
number = 0;
}
void Update(){
//Time counter
myTime+=Time.deltaTime;
//Is it the time to start countdown
if(myTime >= startAfterSeconds && !canSpawn){
canSpawn = true;
myTime =1000000.0f;
}
//Spawn one countdown object each second by the order
if(myTime >= 1.0f && canSpawn){
myTime = 0.0f;
//If countdown is over, then distroy the object.
if(number >= countdown.Length){
SelfDestroy();
} else {
SpawnCountDown( number );
}
number++;
}
}
//Self destroy function
void SelfDestroy ()
{
Destroy (gameObject);
}
//Spawn countdown object at current position
void SpawnCountDown(int number){
GameObject countNum = Instantiate(countdown[number], myTrans.position,myTrans.rotation) as GameObject;
countNum.transform.parent = myTrans;
}
}
as I said the syntax is to stop all animation and sound itself it is like a pause scena, but this does not stop the Update of a script. could not tell you where to put it, besides not specifically what you want in if ..... this syntax is just a standard code to stop and if quires stop something specific you have to look into that in espesifico for example if music have to put stop ("name gives track"), so you can not help much. sorry
Your answer
Follow this Question
Related Questions
How do I keep the scene pause until the prefab animations are done counting down in the scene 0 Answers
How do play prefab animation countdown numbers in my scene properly 0 Answers
Empty animation clips when copying to Prefab 1 Answer
Using animations in prefabs 1 Answer
Prefab connection permanently broken for this scene 1 Answer