- Home /
Load next level when reach score
Hello, i have a script game manager that has a purpose to load the next level after reaching the certain number of score..each level has a different score.
But, the problem is, im confusing and still noob in programming, the score are not reset when reach the next level. I Mean, the score from earlier level still count. Maybe you can see the script exactly in Void SetCountText() how to correct that one.
If anyone know's how to fix this please answer here I really appreciated that Thanx a lot
Here are the script, there are 2 scripts that related.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public static GameManager instance = null;
public GameObject scoreTextObject;
public Text WinText;
int score;
Text scoreText;
// Use this for initialization
void Awake () {
if (instance == null)
instance = this;
else if (instance != null)
Destroy (gameObject);
scoreText = scoreTextObject.GetComponent<Text> ();
SetCountText ();
WinText.text = "";
}
public void Collect(int passedValue, GameObject passedObject)
{
passedObject.GetComponent<Renderer>().enabled = false;
passedObject.GetComponent<Collider> ().enabled = false;
Destroy (passedObject, 0.3f);
score = score + passedValue;
SetCountText ();
}
void SetCountText ()
{
scoreText.text = "" + score.ToString ();
if (score >= 100) {
WinText.text = "Congrats \nGo To The Next Level";
Invoke( "NextLevel", 3.0f );
}
if (score >= 200) {
WinText.text = "Congrats \nGo To The Next Level";
Invoke ("NextLevel1", 3.0f);
}
if (score >= 300) {
WinText.text = "Congrats \nYou Win";
Invoke( "NextLevel2", 3.0f );
}
}
void NextLevel ()
{
score=0;
Application.LoadLevel ("Test6");
}
void NextLevel1 ()
{
score=0;
Application.LoadLevel ("Test7");
}
void NextLevel2 ()
{
Application.LoadLevel ("Win");
}
}
using System.Collections; using UnityEngine;
public class Collectable : MonoBehaviour {
public int value;
public float rotateSpeed;
void Update()
{
gameObject.transform.Rotate (Vector3.up * Time.deltaTime * rotateSpeed);
}
void OnTriggerEnter()
{
GameManager.instance.Collect (value, gameObject);
AudioSource source = GetComponent<AudioSource> ();
source.Play ();
}
}
Answer by Kamil1064 · May 16, 2017 at 05:34 PM
Hi @sunwinki, Version of unity is always nice looking in description :) but try to add this:
using UnityEngine.SceneManagement;
And later in your void NextLevel1():
SceneManager.LoadScene(0);
// or
SceneManager.LoadScene("winScene");
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html