- Home /
How I do load scene by Singleton Patten Score and Countdown Timer in Unity # C
I am having a problem loading a scene by Countdown timer and Score . The score script is in singleton pattern . I also have two ui text in the scene . I want the scene to change if the countdown reach zero and the score have to be below number I have set on the Game script . If the score is above the number I have is set for on the game script and timer reaches zero then the scene don't change. I don't have any errors . I don't know what to do from here. I change the operators around in the game script . I still could get the scenes to change. I am accessing the score script and the countdown timer script from the game script. Here are my scripts :
using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement; public class Game : MonoBehaviour {
private MyClockScript myClock; void Start () { myClock = GetComponent(); }
// Update is called once per frame
void Update () {
if (myClock.m_leftTime < 0 || GameManagerSingleton.score > 0)
{
SceneManager.LoadScene("ui");
}
}
}
using UnityEngine; using System.Collections; using UnityEngine.UI; public class MyClockScript : MonoBehaviour { public int Minutes = 0; public int Seconds = 0;
private Text m_text;
public float m_leftTime;
private void Awake()
{
m_text = GetComponent<Text>();
m_leftTime = GetInitialTime();
}
public void Update()
{
if (m_leftTime > 0f)
{
// Update countdown clock
m_leftTime -= Time.deltaTime;
Minutes = GetLeftMinutes();
Seconds = GetLeftSeconds();
// Show current clock
if (m_leftTime > 0f)
{
m_text.text = "Time : " + Minutes + ":" + Seconds.ToString("00");
}
else
{
// The countdown clock has finished
m_text.text = "Time : 0:00";
}
}
}
private float GetInitialTime()
{
return Minutes * 60f + Seconds;
}
private int GetLeftMinutes()
{
return Mathf.FloorToInt(m_leftTime / 60f);
}
private int GetLeftSeconds()
{
return Mathf.FloorToInt(m_leftTime % 60f);
}
}
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class GameManagerSingleton : MonoBehaviour { public static GameManagerSingleton instance = null;
public static int score;
private Text text;
void Awake()
{
text = GetComponent <Text> ();
score = 0;
if (instance != null && instance != this)
Destroy(gameObject); // Ensures that there aren't multiple Singletons
instance = this;
}
void Update()
{
text.text = "Score: " + score;
Debug.Log("Score: " + score);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612082849im_/https://answers.unity.com/themes/thub/images/avi.jpg)