- Home /
Slider to modify a countdown time in another scene ?
Hi there! I'm pretty new to this and I am developing a simple game of few scenes. In the first there are the instructions, while in the second there is the actual game (in which a task needs to be carried out within a certain amount of time, otherwise it opens the game over scene). In the instructions scene I decided to create a slider to be able to adjust the countdown time.
Here, there is how I modified the slider (image), while below there is the code of the script (TimerChangeScene).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TimerChangeScene : MonoBehaviour
{
public string levelToLoad;
public float timer;
private Text timerSeconds;
// Start is called before the first frame update
void Start()
{
timerSeconds = GetComponent<Text>();
}
// Update is called once per frame
void Update ()
{
timer -= Time.deltaTime;
timerSeconds.text = timer.ToString("f2");
if (timer <= 0)
{
Application.LoadLevel(levelToLoad);
}
}
public void AdjustTimer (float newTimer)
{
timer = newTimer;
}
Can someone help me?
Answer by Deutschland1000 · Aug 06, 2020 at 09:45 AM
The way I understood your question, you want the timer to start when the second scene is loaded. Therefore you would need to put the Update function of your script into some script that is on some GameObject in the second scene.
There are a few ways to share variables across several scenes. The easiest one would be to make a public static variable, which is accessible to all Scripts in all scenes. Another way is to use PlayerPrefs.SetFloat() on the slider and PlayerPrefs.GetFloat() in the Start Method of your other scene. The advantage of this is that the values are saved even after closing the game and restarting.
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Slider))]
public class SliderScript : MonoBehaviour
{
private Text timerSeconds;
//Variable for Method 1
public static float timerDuration;
// Start is called before the first frame update
void Start()
{
timerSeconds = GetComponent<Text>();
//Method 2:
GetComponent<Slider>().SetValueWithoutNotify(PlayerPrefs.GetFloat("timerDuration"));
}
public void AdjustTimer(float newTimer)
{
timerSeconds.text = newTimer.ToString("f2");
//Method 1:
timerDuration = newTimer;
//Method 2:
PlayerPrefs.SetFloat("timerDuration", newTimer);
}
}
using UnityEngine;
using UnityEngine.SceneManagement;
public class Scene2Timer : MonoBehaviour
{
private float timer;
void Start()
{
//Method 1
timer = SliderScript.timerDuration;
//Method 2
timer = PlayerPrefs.GetFloat("timerDuration");
}
void Update()
{
timer -= Time.deltaTime;
if(timer <= 0)
{
SceneManager.LoadScene("GameOverScene");
}
}
}
Thank you a lot for your answer! Actually I had to make some modifications in order to make it functions. Here are the changes for anyone with a similar problem...
using UnityEngine;
using UnityEngine.UI;
[RequireComponent(typeof(Slider))]
public class SliderScript : $$anonymous$$onoBehaviour
{
public static float timerDuration;
// Start is called before the first frame update
void Start()
{
}
public void AdjustTimer(float newTimer)
{
timerDuration = newTimer;
}
}
using UnityEngine;
using UnityEngine.UI;
public class TimerChangeScene : $$anonymous$$onoBehaviour
{
public string levelToLoad;
private float timer;
private Text timerSeconds;
// Start is called before the first frame update
void Start()
{
timer = SliderScript.timerDuration;
timerSeconds = GetComponent<Text>();
}
// Update is called once per frame
void Update ()
{
timer -= Time.deltaTime;
timerSeconds.text = timer.ToString("f2");
if (timer <= 0)
{
Application.LoadLevel(levelToLoad);
}
}
}