I want require points for the level . Right now I have it set for 100 point . The problem I am have is the scene is switch right when I reach 100 . The player a least got to have a 100 or what the amount in to move on to the next level. I also have a time and the timer gotta be done and the player has to have 100 or what points I have in the code before the timer is done. The timer is basically check to see if I have those points or not before the timer is done . The player can score as my points as long as if the player got the require points before the timer is done. I hope this make sense here is my code :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TestScript : MonoBehaviour
{
public MyClockScript myClock;
public ScoreManagerScript scoremanager;
public int scoreToReach = 100; // change this value to what you want
public string nextScene = "FY"; // change this value to what you want
void Update ()
{
if (myClock.m_leftTime <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name); // reload the current scene because ran out of time
}
else if ((ScoreManagerScript.score >= scoreToReach) && (nextScene != "FY"))
{
SceneManager.LoadScene(nextScene);
}
}
}
You could use the Invoke
function.
When the level starts, you can use Invoke
like this:
void NewLevel() {
Invoke("CheckReachedScore", clockTime);
}
void CheckReachedScore()
{
if (scoremanager.score >= scoreToReach) {
// It has reached the score
} else {
// It has not
}
}
Your code is a little messy, but I hope made my point giving the general idea of how to handle this.
You’re saying in the else if statement that if the score is greater than OR IQUAL to 100, change the level.
I would try something like this e.g.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TestScript : MonoBehaviour
{
public MyClockScript myClock;
public ScoreManagerScript scoremanager;
public int scoreToReach = 100;
void Update ()
{
if (myClock.m_leftTime <= 0)
{
SceneManager.LoadScene("thisScene");
}
//I don't like the else if statement...
if (ScoreManagerScript.score == scoreToReach)
{
//Print some GUI message here
Debug.Log("Press Enter to change level or keep playing");
}
if ((Input.GetKeyDown(KeyCode.Return)) && (ScoreManagerScript.score >= scoreToReach))
{
SceneManager.LoadScene("nextScene");
}
}
}
Do you mean you always want the counter to run down to zero before checking the score?
if so, something like this should work
if (myClock.m_leftTime <= 0)
{
if ((ScoreManagerScript.score >= scoreToReach) && (nextScene != "FY"))
{
SceneManager.LoadScene(nextScene);
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
hope that helps,