Hi, I have a problem dealing with time in this line of code:
Time.timeSinceLevelLoad % levelUpBySeconds == 0f;
It works perfectly if I’m in the scene with that script in empty gameObject called “GameManager” in “Level_Start” scene as my starting scene, but when it came from other scene (“Start_Scene”) and load it or load it again ( Application.LoadLevel("Level_Start")
/ using UnityEngine.SceneManagement;
SceneManager.LoadScene("Level_Start")
) – it does not work anymore. Why is this happening?
Is Time.time
or Time.timeSinceLevelLoad
loses its precision in calculations when Application.LoadLevel() is done? or is the problem with modulus ( %
)?
My “Level_Start” scene:
You see, when I play this scene with my editor, this works properly; let’s say, I set my public preference “levelUpBySeconds” to 10, it means every 10 secs will update my “LevelValue” in hierarchy added by 1, and also instantiate 1 “EnemySpawn” prefab every level rises by 1: (working properly)
My “Start_Scene” scene: if I start here or go here…
…and go to “Level_Start” scene (again) by Application.LoadLevel(); now the “LevelValue” stays at 1 forever: (not working)
Please just look for this line below:
if (Time.timeSinceLevelLoad % levelUpBySeconds == 0f && Time.timeSinceLevelLoad > 0f)
in line 38 (and also 54 which have no problem unlike line 38) of GameController.cs.
Note: I have tried Time.time - timeElapse % levelUpBySeconds == 0f
where timeElapse is a variable initiate from start with Time.time, or Mathf.Repeat(Time.timeSinceLevelLoad, levelUpBySeconds) == 0
still the same result…
GameController.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class GameController : MonoBehaviour {
public float levelUpBySeconds = 60f;
public int levelUpByScore = 0;
[Range(1,100)] public int level = 1;
public Transform parentInstantiatorPrefab;
public Transform enemySpawnPrefab;
void Start () {
if (level > 0)
{
SetLevel();
for (int i = 1; i < level; i++ )
{
SetEnemySpawn();
}
}
/*else //uses "[Range(1,100)]" instead... works like a charm! ;)
{
level = 1;
SetLevel();
}*/
}
void FixedUpdate () {
SetLevelByTime();
if (Input.GetKeyUp(KeyCode.Escape)) {
Application.LoadLevel("Start_Scene");
}
}
void SetLevelByTime()
{
GameObject.Find("TimeValue").GetComponent<Text>().text = System.Math.Round(Time.timeSinceLevelLoad, 2).ToString();
if (levelUpBySeconds > 0f)
{
if (Time.timeSinceLevelLoad % levelUpBySeconds == 0f && Time.timeSinceLevelLoad > 0f)
{ /*### ^up PLEASE SEE this if statement what's going on here? ###*/
level++;
SetLevel();
}
if (Time.timeSinceLevelLoad == 10) //Edited: I just add this line of code to see...
Debug.Log(Time.timeSinceLevelLoad.ToString()); //As mention, does not work/display when loaded from other Scene as start scene!
if (Time.timeSinceLevelLoad == 20) //Edited: I just add this line of code to see...
Debug.Log(Time.timeSinceLevelLoad.ToString()); //As mention, does not work/display when loaded from other Scene as start scene!
}
}
public void SetLevelByScore(int currentScore)
{
if (levelUpByScore > 0f)
{
if (currentScore % levelUpByScore == 0 && Time.timeSinceLevelLoad > 0f)
{ /*### ^up PLEASE SEE ALSO this if statement, it works perfectly as we expected! ###*/
level++;
SetLevel();
}
}
}
void SetLevel ()
{
GameObject.Find("LevelValue").GetComponent<Text>().text = level.ToString();
SetEnemySpawn();
}
void SetEnemySpawn()
{
Instantiate(enemySpawnPrefab, parentInstantiatorPrefab);
}
}
ScoreController.cs (Extra, just look, no problem here)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ScoreController : MonoBehaviour {
private int currentScore = 0;
private int highScore = 0;
void Start ()
{
if (PlayerPrefs.HasKey("highScore"))
{
highScore = PlayerPrefs.GetInt("highScore");
}
else
{
PlayerPrefs.SetInt("highScore", 0);
}
GameObject.Find("HighScoreValue").GetComponent<Text>().text = highScore.ToString();
SetScore();
}
public void AddScore ()
{
currentScore++;
if (currentScore > highScore)
{
PlayerPrefs.SetInt("highScore", currentScore);
}
SetScore();
}
void SetScore()
{
GameObject.Find("ScoreValue").GetComponent<Text>().text = currentScore.ToString();
GameObject.Find("GameManager").GetComponent<GameController>().SetLevelByScore(currentScore);
}
}
StartSceneController.cs (Extra, just look, no problem here)
using UnityEngine;
using System.Collections;
public class StartSceneController : MonoBehaviour {
void Update () {
if (Input.GetKeyUp(KeyCode.Return) || Input.GetKeyUp(KeyCode.Mouse0)) {
Application.LoadLevel("Level_Start");
}
if (Input.GetKeyUp(KeyCode.Escape)) {
Application.Quit();
}
}
}