End Game error from Brackeys tutorial.

Hey, I am following along a Brackeys tutorial of how to make a game in Unity (ep.8) , and I am just getting into game dev.

In the console, it says endgame as soon as the player touches the ground. However, I need it to show endgame when it actually hits one of the obstacles or falls over the edge. Here is the code for the GameManager, [then] PlayerCollision, [then] PlayerMovement (btw, the codes are on three different scripts):

using UnityEngine;

public class GameManager : MonoBehaviour
{

bool gameHasEnded = false;

public void EndGame()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log(“GAME OVER”);
}
}

}

using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
public PlayerMovement movement;
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == “Obstacle”)
{
movement.enabled = false;
FindObjectOfType().EndGame();
}
}
}

using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public Rigidbody rb;
public float forwardForce = 2000f;
public float sidewaysForce = 500f;
// Update is called once per frame
void FixedUpdate ()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if ( Input.GetKey(“d”))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}

if (Input.GetKey(“a”))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (rb.position.y > -2f)
{
FindObjectOfType().EndGame();
}
}
}

CHECK OUT THIS SCREENSHOT OF UNITY IN PLAY MODE AND THE CONSOLE

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.

Remember, code is only a tiny part of the problem. Assets and scenes and prefabs have to be set up just right as well, so you can use code to figure out it something isn’t right.

Once you find something specific, if you have more questions, here is how to report problems productively in the Unity3D forums:

http://plbm.com/?p=220

@Kurt-Dekker I already put Debug.Log statements in, that’s my whole point. I only want it to show a Debug.Log statement at a specific time (when the main object either hits another object (except for the ground), or when the main object falls over the edge).

How come it shows it when it touches the ground? Also, I followed every step in Brackeys’ video.

Also, there is absolutely no way of researching this problem, as it is not an error, and the game seems to think that it is doing everything that it’s supposed to be doing.

Never mind, it was only because I put a < instead of > hahahahahahahahahaha, when it worked I laughed so hard

I can investigate any problem, all the way down to a bit. If there is a 0 and it is supposed to be a 1, I can find out why.

Everything else above that just gets easier. :slight_smile:

“If people built houses the way we write programs, the first woodpecker would wipe out civilization.”

As seen on : https://en.wikiquote.org/wiki/Programming

I am having the same problem can anyone help?

1 Like