Level restarts when player hits the ground problem.

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.

Not sure how to help without seeing the script.

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);
}
}

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: Using code tags properly

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!

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.

So, you think the problem is not in the script?

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.

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.

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().enabled = true;
FindObjectOfType().EndGame();
FindObjectOfType().EndGame();
}
}

}
This is my code for collision.