Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Level restarts when player hits the ground problem.

Discussion in 'Scripting' started by radajz, Mar 12, 2021.

  1. radajz

    radajz

    Joined:
    Mar 6, 2021
    Posts:
    9
    Hello everyone, so I recently started with game development and I managed to make a few games that were really bad, but we won't talk about it now.
    I made a brand new game and added a lot of scripts for the camera, movement etc. So, the problem is that I wrote a script that when a player hits an object, the game restarts. The code works nicely, BUT the game also restarts when the player hits the ground (which is undesirable). I need help with that as soon as possible. Thank you!
    P.S. I'm making 3D game.
     
    Last edited: Mar 12, 2021
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Not sure how to help without seeing the script.
     
  3. radajz

    radajz

    Joined:
    Mar 6, 2021
    Posts:
    9
    using UnityEngine;
    using UnityEngine.SceneManagement;

    public class GameManager : MonoBehaviour
    {

    bool gameHasEnded = false;

    public float restartDelay = 1f;

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

    }

    void Restart ()
    {
    SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }
    }
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    There is no contact / collision code above, eg the code that calls EndGame().

    Find where that is and use either layers and layermasks, or perhaps tags, or some unique way of identifying that something SHOULD restart versus something should NOT restart. You can also look for some tutorials on tags/layers/detecting collisions.

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  5. radajz

    radajz

    Joined:
    Mar 6, 2021
    Posts:
    9
    Hi, Kurt! Thank you for your reply. So, I'm newbie, and I dont know that much about coding in C#. Could you please send the correct code or correct my code? Thank you!
     
    Last edited: Mar 13, 2021
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    No, I cannot send you code, that's not how this works.

    I directed you in how to investigate further.

    If you need more information, I suggested trying some tutorials on collisions.

    Besides, it is probably NOT code but rather setup. Code is a tiny part of the problem usually. Most of it is setup.
     
  7. radajz

    radajz

    Joined:
    Mar 6, 2021
    Posts:
    9
    So, you think the problem is not in the script?
     
  8. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    you show the code that has nothing to do with hit, Kurt-Dekker want you to investigate anything to do with hit and where EndGame() been use.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Code is about 10% of the problem. The rest is design and setup.

    Sounds like you're perhaps in need of working through a few more tutorials to sort stuff out.

    If this is not something you're willing to do, then perhaps you can get success with this approach:

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run?
    - what are the values of the variables involved? Are they initialized?

    Knowing this information will help you reason about the behavior you are seeing.

    If you cannot get success with that, I assure you nobody here will be able to do any better.
     
  10. radajz

    radajz

    Joined:
    Mar 6, 2021
    Posts:
    9
    Thank you for reply! I think the problem is here
    using UnityEngine;

    public class PlayerCollision : MonoBehaviour {

    public PlayerMovement movement;

    void OnCollisionEnter(Collision collisionInfo)
    {
    if (collisionInfo.collider.tag == "Obstacle");
    {
    GetComponent<PlayerMovement>().enabled = true;
    FindObjectOfType<GameManager>().EndGame();
    FindObjectOfType<GameManager>().EndGame();
    }
    }

    }
    This is my code for collision.
     
    Last edited: Mar 14, 2021