Game freezes when restarting the game
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");
}
Your answer
Follow this Question
Related Questions
increase score with a variable from another script 1 Answer
Generating random points and instantiating prefabs with a set distance 0 Answers
How to set a variable of a non-monobehavior script from a monobehaviour script 0 Answers
UI buttons won't recognize script 0 Answers
I create material with script but it does not render right 0 Answers