Search Unity

Question GameOver screen doesn't appear when player hitbox collides with obstacle hitbox

Discussion in 'Scripting' started by J-Games99, Feb 3, 2023.

  1. J-Games99

    J-Games99

    Joined:
    Jan 31, 2023
    Posts:
    3
    Hello, I am very new to Unity and have just recently started my journey into coding. I used flappy bird tutorial and it had a game over function and it went perfectly, however I tried the same GameOver code into my own game and it has not been working, whenever the player hits the obstacle it doesn't show the GameOver screen. It's not telling me that there is an error, the most it tells me is a warning stating: "Assets\PlayerScript.cs(41,15): warning CS8321: The local function 'OnCollisionEnter2D' is declared but never used" >! At first, I rechecked the coding to find an error, but I found nothing that I can see, I have looked this up and have tried to find an answer but to no avail. It would be greatly appreciated if someone would be able to tell me the issue. I'm sorry if this was a dumb question, but I am VERY new. Please help me and thank you.
    Here is the code for the LogicScript (Called LogiScore in my code):


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class LogiScore : MonoBehaviour
    9. {
    10.     public int playerScore;
    11.     public Text scoreText;
    12.     public GameObject GameOverScreen;
    13.  
    14.  
    15.     public void addScore()
    16.     {
    17.         playerScore = playerScore + 1;
    18.         scoreText.text = playerScore.ToString();
    19.     }
    20.     public void restartGame()
    21.     {
    22.         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    23.     }
    24.  
    25.     public void gameOver()
    26.     {
    27.         GameOverScreen.SetActive(true);
    28.     }
    29.    }
    Here is the code for the player:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour
    7. {
    8.  
    9.     public float speed = 10.5f;
    10.     public LogiScore logic;
    11.  
    12.  
    13.     void Start()
    14.     {
    15.         logic = GameObject.FindGameObjectWithTag("logic").GetComponent<LogiScore>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         Vector3 pos = transform.position;
    22.  
    23.         if (Input.GetKeyDown("w"))
    24.         {
    25.             pos.y += speed * Time.deltaTime;
    26.         }
    27.         if (Input.GetKeyDown("s"))
    28.         {
    29.             pos.y -= speed * Time.deltaTime;
    30.         }
    31.         if (Input.GetKeyDown("d"))
    32.         {
    33.             pos.x += speed * Time.deltaTime;
    34.         }
    35.         if (Input.GetKeyDown("a"))
    36.         {
    37.             pos.x -= speed * Time.deltaTime;
    38.         }
    39.  
    40.         transform.position = pos;
    41.  
    42.          void OnCollisionEnter2D(Collision2D collision)
    43.         {
    44.  
    45.             logic.gameOver();
    46.         }
    47.     }
    48. }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,930
    You've declared your
    OnCollisionEnter2D
    inside your Update method. You need to move it outside of other methods.