I'm trying to write a simple script, but the last part of the code doesn't work.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    private Rigidbody playerRb;
    public float speed = 4f;
    public float jumpForce = 12;
    public float gravityModifier;
    public bool isOnGround = true;
    public bool gameOver = false;
    public float verticalInput;
    public float horizontalInput;
    public float zUpperBound = 1.3f;
    public float zLowerBound = -1.3f;
    private float xBound = 10;
    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody>();
        Physics.gravity *= gravityModifier;
        
    }

    // Update is called once per frame
    void Update()
    {
        

        if(Input.GetKeyDown(KeyCode.Space) && isOnGround && !gameOver)
        {
          playerRb.AddForce(Vector3.up * jumpForce , ForceMode.Impulse);
          isOnGround = false;
        }

        verticalInput = Input.GetAxis("Horizontal");
        transform.Translate(Vector3.forward * Time.deltaTime * verticalInput * speed);

        if (transform.position.z < zLowerBound)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, zLowerBound);    
        }
        if (transform.position.z > zUpperBound)
        {
            transform.position = new Vector3(transform.position.x, transform.position.y, zUpperBound);
        }
        
    }

    private void OnCollisionEnter(Collision collision)

    {
       if (collision.gameObject.CompareTag("Ground"))
       {
        isOnGround = true;
       }
    }
    
    public void GameOver()
    {
        if(transform.position.x > xBound && !gameOver) 
        {
            gameOver = true;
            Debug.Log("Game Over!");
        } 
    }   
}

after the player’s position becomes greater than the given border, nothing happens. BUT! When I paste the last three lines of code into the Update method everything works as it should. I have already changed the access of my GameOver method to both public and private, the program does not give errors

I’m not sure if you’re new to coding, or just having massive coders block. But you make a function called “GameOver()”, which in itself is a check. But you don’t put it in your Update() function, so the compiler never reads it. So putting GameOver(); within your Update() function will allow it to work as intended.

Unless you mean to say that the GameOver() function is read elsewhere, in another script? If so, then yes it would need to be public, so the other script can see it. But if using within the same script, having it as a blank void, or private, or public wouldn’t matter in any way shape or form.

Thank you for the answer. I am currently working independently on a prototype of a simple game based on already made training projects from Unity, but there was no new GameOver() method in the training scripts, they just change the value from false to true. Perhaps this is due to the use of the OnCollisionEnter(Collision collision) method, but I haven’t figured it out yet. And if I remember correctly, this method was not specified in Update().