- Home /
How do I code time based score unity3d
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);
}
}
}
Answer by Happy-Zomby · Aug 13, 2016 at 09:44 PM
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,
Yes . That what I mean . The problem is that is only went off of this code :
When it reach zero it reset the scene . It's didn't check for points
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name);
Answer by afonsolfm · Aug 13, 2016 at 11:04 PM
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.
I got the scene to switch when the points reach and the timer reaches zero. I only need an else statement so I can switch scenes when the points are less when timer reach zero . Here is what I got right now for code :
When I get a lesser score than score reach plays this code :
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name);
I need this part here in a else statement the scene switch in the if statement:
if (myClock.m_leftTime <= 0)
{
if ((Score$$anonymous$$anagerScript.score >= scoreToReach) && (nextScene != ""))
{
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Scene$$anonymous$$anagement;
public class TestScript : $$anonymous$$onoBehaviour
{
public $$anonymous$$yClockScript myClock;
public Score$$anonymous$$anagerScript scoremanager;
public int scoreToReach = 99; // change this value to what you want
public int leastscore = 50;
public string nextScene = "FY"; // change this value to what you want
void Update ()
{
if (myClock.m_leftTime <= 0)
{
if ((Score$$anonymous$$anagerScript.score >= scoreToReach) && (nextScene != ""))
{
Scene$$anonymous$$anager.LoadScene(nextScene);
}
else
{
Scene$$anonymous$$anager.LoadScene(Scene$$anonymous$$anager.GetActiveScene().name);
}
}
}
}
Answer by FabriBertani · Aug 13, 2016 at 11:05 PM
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");
}
}
}