- Home /
Game freezes after restarting.
the game I made through a tutorial was fine but I did some tweaks so the game can be playable in a better way. I added a start and a restart scenes to the project but when restarting the game it just freezes, I looked for the problem in the internet and found that I need to change the static variables, but I don't know how, so could you please help me fixing it, I've been using unity for a while but still can't figure out the scripting world. here are my scripts:
public class StartGame : MonoBehaviour {
public void StartLevel()
{
SceneManager.LoadScene("Main");
}
}
public class DragonScript : MonoBehaviour {
public bool isAlive;
Rigidbody2D myRigidbody;
Animator myAnimator;
float jumpForce;
void Awake() { DontDestroyOnLoad(transform.gameObject); }
// Use this for initialization
void Start () {
isAlive = true;
myRigidbody = gameObject.GetComponent<Rigidbody2D>();
myAnimator = gameObject.GetComponent<Animator>();
jumpForce = 10f;
myRigidbody.gravityScale = 5f;
}
// Update is called once per frame
void Update () {
if (isAlive)
{
if (Input.GetMouseButton(0))
{
Flap();
}
CheckIfDragonVisibleOnScreen();
}
}
void Flap()
{
myRigidbody.velocity = new Vector2(0, jumpForce);
myAnimator.SetTrigger("Flap");
}
void OnCollisionEnter2D(Collision2D target)
{
if (target.gameObject.tag == "Obstacles")
{
isAlive = false;
Time.timeScale = 0f;
}
}
void CheckIfDragonVisibleOnScreen()
{
if (Mathf.Abs(gameObject.transform.position.y) > 5.5f)
{
isAlive = false;
Time.timeScale = 0f;
SceneManager.LoadScene("Restart");
}
}
}
public class RestartGame : MonoBehaviour {
// Use this for initialization
public void RestartLevel () {
SceneManager.LoadScene("Start");
}
Answer by nygren · Nov 19, 2017 at 01:12 AM
My guess is that you've forgotten to change back Time.timeScale to something other than 0 after restarting.
I suggest you have a method in that sets up everything that needs to be setup when starting a level; isAlive, Time.timeScale, etc. Where you place it and call it is up to you. I guess StartLevel and RestartLevel are candidates to call it.
So u mean like after the loadscene code in the start and restart classes I put Time.timeScale with a value ??
Answer by whatevergeek · Jan 31, 2019 at 01:11 PM
are you using the latest version of .net framework? if yes, this is what fixed my problem:
in your TimeManager.cs, change "return false" to "yield break" as follows:
IEnumerator FadeTo(float value, float time)
{
for (float t = 0f; t < 1; t += Time.deltaTime / time)
{
Time.timeScale = Mathf.Lerp(Time.timeScale, value, t);
if(Mathf.Abs(value - Time.timeScale) < .01f)
{
Time.timeScale = value;
yield break;
}
yield return null;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers