Home > Net >  Unity UI Button Doesn't Change Scene When Clicked
Unity UI Button Doesn't Change Scene When Clicked

Time:09-30

I am creating a game project with a main menu and 3 other scenes (MyWorld, ImportWorld, Lab). I've made the buttons on the main menu clickable to go the 3 other scenes, and want to make a back button to return to the main menu from the 3 scenes. I managed to make the back button work on the Lab scene, but when I tried the same steps and scripts to the MyWorld and ImportWorld, it doesn't bring to the main menu although they are clickable and changes color when hovered and clicked.

Below is the current script I am using for the Lab scene which is working. However I had a problem when I wanted to change the name of the text, suddenly it didn't worked. But luckily I didn't save and just undo which makes the button workable again.

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

public class ReturnMainMenu : MonoBehaviour
{
    public void returnMenu()
    {
        SceneManager.LoadScene("MainScene");
    }
}

Below is the script I used for the Main Menu scene which opens up the 3 other scenes and works fine.

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

public class ButtonTrigger : MonoBehaviour
{
    public void btnMyWorld() //function when clicked, open My World scene
    {
        SceneManager.LoadScene("MyWorld");
    }

    public void btnImportWorld() //function when clicked, open Import World scene
    {
        SceneManager.LoadScene("ImportWorld");
    }

    public void btnLab() //function when clicked, open Lab scene
    {
        SceneManager.LoadScene("Lab");
    }

    public void btnQuit()
    {
        Application.Quit();
    }

    public void btnReturnMainScene() //function when clicked, should return to Main Menu
    {
        SceneManager.LoadScene("MainScene");
    }
    //tried adding this function to the Back buttons but didn't work
}

I've tried various scripts (including entering scene index and scene name), changed the canvas position in hierarchy to be on top, have Event System and RayCast target but nothing seems to fix the problem. Does anyone know what might be the cause that makes the button clickable, changes color when hover but doesn't load the Main Menu scene? And as I mentioned I had problems with the workable back button at Lab scene which stops working when I changed my text name, so maybe does it have anything to do with the project settings/scripts?

CodePudding user response:

Maybe you didn't put the scene in build setting.

CodePudding user response:

As in my understanding from the information provided, you have established a working code, which you use in multiple areas and scenes.

Understanding from the issue you are presented:

  1. A button has been established and is clickable Returning a colour change to prove it.
  2. The text your tried to change is about the scene change within your script.

    public class ReturnMainMenu : MonoBehaviour
    {
        public void returnMenu()
        {
            UnityEngine.SceneManagement.SceneManager.LoadScene("MainMenu");
            //Has to be written exactly as the Scene you are aiming to load up.
            //Change the steps to include SceneManagement.
        }
    }

Look up if it is written down correctly keeping Case sensitivity in mind. Check if the scene is added to your Build settings enter image description here

Make sure the button has "Interactable" as checked (Ticked on) If you copied the button from another scene, within unity more often than not there will be issues with it that it won't work as intended, for that I recommend making a copy of the button you wish to use and make a prefab out of it. (Which can be used in every area that you desire and scenes you desire).

  • Related