- Home /
Time is not stopping
Hello,
I had this script in hand but it seems there is a new problem in here.
So I play the game and collect the objects but the time is not stopping and it just keeps going I get the debug saying I "found everyone" but it is not moving to the next scene. TimesUp is working fine though.
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
public class Aika : MonoBehaviour
{
public Text timerText;
private float startTime;
private IEnumerator coroutine;
public float t;
public float timesUp;
public int points = 0;
public int winTotal = 2;
public GameManager gameManager;
private void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 20), "Löydetty : " + points);
}
void Start()
{
startTime = Time.time;
Time.timeScale = 1.0f;
timesUp = 300f;
StartCoroutine(Timer());
}
IEnumerator Timer()
{
Debug.Log("Coroutine online");
yield return new WaitForSeconds(300f);
gameManager.EndGame();
}
void Update()
{
float t = Time.time - startTime;
string minutes = ((int)t / 59).ToString("00");
string seconds = (t % 59).ToString("00");
timerText.text = minutes + ":" + seconds;
if (winTotal == points)
{
Debug.Log("Found everyone");
return;
}
if (t == timesUp)
{
Debug.Log("Times Up!");
return;
}
}
}
Also just in case gonna put the GameManager script too.
using UnityEngine;
public class GameManager : MonoBehaviour
{
public Game_Over game_Over;
public Aika winTotal;
public Aika points;
public Aika timesUp;
void Start()
{
}
public void Loysit_Kaikki ()
{
if (winTotal == true)
{
Debug.Log("Löysit kaikki!");
game_Over.QuitGame(); //Moves to the Game_Over script
}
}
public void EndGame()
{
if (timesUp == true)
{
Debug.Log("Continue!");
game_Over.Continue(); //Moves to the Game_Over script
// put restart game here later.
}
}
}
Also the last piece Game_Over script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Game_Over : MonoBehaviour
{
public IEnumerator coroutine;
public GameManager gameManager;
float continueButton;
private void Start()
{
Cursor.visible = true;
}
void Update()
{
}
public void Continue()
{
Debug.Log("End Scene loaded");
Time.timeScale = 0; //stops time and cant move the character.
SceneManager.LoadScene("EndScene");
}
public void QuitGame()
{
Debug.Log("Quit!");
Time.timeScale = 0;
SceneManager.LoadScene("StartScene");
}
public void pushKylla()
{
SceneManager.LoadScene("TheGame");
Debug.Log("Kyllä painettu");
}
}
So what do? Thank you in advance.
Good day.
I'm confused, maybe its my low scripting knowledge
First, i dont get why you do just return in the Update (a void method) when there isnt anymore return that returns somethig.
Second at Game$$anonymous$$anager script, i dont get why you define variables time wins and points as a Aika variables and (you doing via inspector?) Why they are not int / float / bool ?
And more...
Answer by pako · Jan 30, 2019 at 02:13 PM
You have many bugs in your code, and this forum is not meant for debugging your code. So, I'll just give you some pointers.
public Aika winTotal;
public Aika points;
public Aika timesUp;
All these refer to an instance of the Aika script, and not to the individual variables (per their name).
So, if you have dragged-and-droped the Aika script in the Inspector, the if()
condition will always return true, in effect confirming that an Aika
instance exists:
if (winTotal == true) //always true
if (timesUp == true) //always true
You don't show where you increase points
but in Update()
:
if (winTotal == points) // when this becomes true Update() will exit because of the return statement
{
Debug.Log("Found everyone");
return;
}
// so the following code runs only when the above condition (winTotal == points) is false
if (t == timesUp)
{
Debug.Log("Times Up!");
return;
}
game_Over.Continue() should run after 300 seconds of game time, i.e. when `IEnumerator Timer()` calls `gameManager.EndGame()`.
I believe this should help you fix your code.
Ok, I give it a try. Where can I put, in the future, similar kind of questions so I don't post here when looking for answers? And thank you. @pako
Here's an excerpt from the FAQ:
Some reasons for getting a post rejected:
Asking us to fix your code: more than likely, you simply need to get a better understanding of basic program$$anonymous$$g. Having a look at the Scripting tutorials on the Learn page will help you become more familiar with scripting in Unity
Nevertheless, many questions on code fixing in "Answers" are tolerated. In this case there were multiple errors in multiple scripts, and at least one relevant script was not posted. I would need to replicate your project in my PC, and step through code in the debugger (something you should really do yourself), in order to give you a complete answer.
Having said that, I$$anonymous$$HO my advice is that you don't post such questions in the future. Fire up the debugger, step through your code, google again and again to better understand any error messages, and figure out the issues yourself. Believe it or not, this is the shortest route to advancing your coding expertise.
Right, I will keep that in $$anonymous$$d. But I got the script working and I will keep working on it some more. Thanks again.
Answer by ray2yar · Jan 30, 2019 at 01:14 PM
As far as I can tell you have no logic to actually move to a different scene once the win condition is met. So... that's your problem.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How Do I check if a Bool was True a few moments ago 2 Answers
How to add a cooldown sort of thing. 1 Answer
Multiple Cars not working 1 Answer
If Event Keeps Occurring frequently then Do Something 0 Answers