How to make Game Over UI pop up after a collision?

Viewed 142

I am making a game where, if the catcher gets destroyed by the object, the game over screen is triggered. All that seems to occur is that there is a giant game over the screen at the beginning of when I play, while the game is running in the background. For some reason, the game does not seem to call in the game over screen only on collision. This is the script I am using for my catcher, where it collides, disappears, and then the game over screen is set up to be triggered.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
    public GameOverScreen GameOverScreen;
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Catcher"))
        {
            Destroy(collision.gameObject);
            GameOverScreen;
        }
    }
}

and this is the code for my GameOverScreen.

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

public class GameOverScreen : MonoBehaviour
{
    public Text pointsText;

    public void Setup(int score) 
    {
        gameObject.SetActive(true);
        pointsText.text = "Score:" + score.ToString(); 
    }
}
1 Answers

You are not calling anything when an object collides, you're just listing the reference of the object. You'll need to call the function that you have exposed. Without calling a method from the script reference, no code will be run. Edit your first snippet as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
    public GameOverScreen GameOverScreen;
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Catcher"))
        {
            Destroy(collision.gameObject);
            GameOverScreen.Setup();
        }
    }
}

The other option would be to move the code in Setup to Awake or Start or OnEnable, then instead of calling the function in the collision, you just need to set it as active.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CatcherDestroy : MonoBehaviour
{
    public GameOverScreen GameOverScreen;
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Catcher"))
        {
            Destroy(collision.gameObject);
            GameOverScreen.score = theScore;
            GameOverScreen.gameObject.SetActive(true);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameOverScreen : MonoBehaviour
{
    public Text pointsText;
    public int score;
    
    private void OnEnable()
    {
        pointsText.text = "Score:" + score.ToString(); 
    }
}

The one issue is you'll need to pass in the score parameter which I do not see in your script CatcherDestroy.

Related