Jump to content
  • Advertisement
KnowItAllGames

Game isnt restarting when i collide with object

This topic is 511 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi i have script that i believe is meant to restart my character/scene when i collide with an Obstacle but it doesnt work!

Quote

using UnityEngine;

public class PlayerCollision : MonoBehaviour {

    public PlayerMovement movement;        


    void OnCollisionEnter (Collision collisionInfo)
    {

        if (collisionInfo.collider.tag == "Obstacle")
        {
            movement.enabled = false;    
            FindObjectOfType<GameManager>().EndGame();

        }
    }

}
    

Quote

using UnityEngine;
using UnityEngine.SceneManagement;

public class gameManager : MonoBehaviour {

    bool gameHasEnded = false;

    public float restartDelay = 1f;

    public GameObject completeLevelUI;

    public void CompleteLevel ()
    {
        completeLevelUI.SetActive(true);
    }

    public void EndGame ()
    {
        if (gameHasEnded == false)
        {
            gameHasEnded = true;
            Debug.Log("GAME OVER");
            Invoke("Restart", restartDelay);
        }
    }

    void Restart ()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

}
 

 

Share this post


Link to post
Share on other sites
Advertisement

What's not working about it? is EndGame getting called at all? is gameHasEnded false when you get into EndGame? is Debug.Log("GAME"); showing "GAME"? is if (collisionInfo.collider.tag == "Obstacle") true or false?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!