Replay Button doesn't seem to work

Hi,
I’m having a similar issue to Replay Button not working
But I’ve looked at the sorting order, and the WinCanvas is set to 100 and QuizCanvas is set to 1.
I tried to make a new button and change the highlighting color and also the onclick color which seemed to work, but as soon as I dragged the game manager onto it, it simply stopped working.

using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    Quiz quiz;
    EndScreen endScreen;

    private void Awake()
    {
        quiz = FindObjectOfType<Quiz>();
        endScreen = FindObjectOfType<EndScreen>();
    }

    private void Start()
    {
        quiz.gameObject.SetActive(true);
        endScreen.gameObject.SetActive(false);
    }

    private void Update()
    {
        if(quiz.isComplete)
        {
            quiz.gameObject.SetActive(false);
            endScreen.gameObject.SetActive(true);
            endScreen.ShowFinalScore();
        }
    }

    public void OnReplayLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

Hi,

Welcome to our community! :slight_smile:

When it comes to computers, ‘not working’ is always a bit vague and tricky. Try to figure out what exactly is not working. Use Debug.Logs for that.

For example, just by looking at the code, it is impossible to tell whether OnReplayLevel() gets called when you click the button. If it does not get called, the code block does not get executed either.

If it does get called, the code block does get executed but if nothing else happens, the issue might be SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);.

Before you waste your time looking in the wrong place, narrow the problem down further. :slight_smile:

I have added the debug method to the OnReplayLevel method, but it’s not called since the button isn’t clickable.

using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    Quiz quiz;
    EndScreen endScreen;

    private void Awake()
    {
        quiz = FindObjectOfType<Quiz>();
        endScreen = FindObjectOfType<EndScreen>();
    }

    private void Start()
    {
        quiz.gameObject.SetActive(true);
        endScreen.gameObject.SetActive(false);
    }

    private void Update()
    {
        if(quiz.isComplete)
        {
            quiz.gameObject.SetActive(false);
            endScreen.gameObject.SetActive(true);
            endScreen.ShowFinalScore();
        }
    }

    public void OnReplayLevel()
    {
        Debug.Log("Test OnReplayLevel method");
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

Great. Now we know that the method does not get called. :slight_smile:

However, we do not know if the button ‘isn’t clickable’. If it wasn’t clickable (according to Unity’s definion), ‘Interactable’ would be disabled in the Button component.

  1. Check if ‘Interactable’ is enabled in the Button component.
  2. Check if the OnClick() field of the Button component references your OnReplayLevel method.
  3. Is there an EventSystem in your Hierarchy? If not, add one.

As you can see in the provided screenshots, intractable is enabled, the OnClick() is connected to my OnReplayLevel method and there is an EventSystem in the hierarchy under QuizCanvas, but does it also need to exist under WinCanvas?


image

Yes, all UI elements must be parented to a game object with a Canvas component. Your screenshots look correct so far.

Here are three suggestions:

  1. Move the EventSystem to the top level of the Hierarchy, so it is not a child of any game object. If the QuizCanvas gets disabled, the EventSystem gets disabled along with it, and the user input will not be processed anymore. Maybe that’s what happens at runtime?

  2. Check the Canvas game object. In the Canvas component, ‘Receives Events’ must be enabled. Furthermore, the Canvas game object needs a GraphicRaycaster component attached.

  3. Since all game objects are enabled, I would suggest to disable all of them but the WinCanvas. This way, you ensure that no other UI element blocks your mouse click. Maybe an invisible is on top of your ReplayButton. Then the mouse clicks the invisible object, and the raycast does not reach the ReplayButton. At least, that’s my conjecture.


If nothing helped, try to delete the EventSystem and recreate it. Do the same for the button. In rare cases, the components break for no apparent reason. Removing and readding sometimes helps.

2 Likes

Moving the EventSystem to the top level of the hierarchy worked.
Thank you for the help.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms