- Home /
The question is answered, right answer was accepted
Unity Newbie: Buttons won't switch scenes
Hey sorry I am a Unity Newbie, but I am stuck. The buttons won't switch to the scenes, the buttons work and the code shows no errors. Someone recommended looking at the build settings, all my scenes are selected, so I am really lost. Please Help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
public class ChangeScene : MonoBehaviour {
// Use this for initialization
/*void Start () {
}
// Update is called once per frame
void Update () {
}*/
//detects which button is clicked
public void detectClick()
{
//get the name of the current clicked object
var theObject = EventSystem.current.currentSelectedGameObject;
//take action depending on which object is clicked
switch (theObject.name)
{
case "Microwave":
Debug.Log(theObject.name);
SceneManager.LoadScene("Coming_Soon");
break;
case "RiceCooker":
Debug.Log(theObject.name);
SceneManager.LoadScene("Coming_Soon");
break;
case "Stove":
Debug.Log(theObject.name);
SceneManager.LoadScene("Stove_Style");
break;
case "S_African":
Debug.Log(theObject.name);
SceneManager.LoadScene("Coming_Soon");
break;
case "S_Eastern":
Debug.Log(theObject.name);
SceneManager.LoadScene("Coming_Soon");
break;
case "S_MiddleEastern":
Debug.Log(theObject.name);
SceneManager.LoadScene("Coming_Soon");
break;
}
}
}
Have you tried to print the name of the object outside of the switch block?
What do you mean by printing the name outside of the switch block?
public void detectClick()
{
//get the name of the current clicked object
var theObject = EventSystem.current.currentSelectedGameObject;
Debug.Log( theObject.name ) ;
// ...
}
This way, you will be able to check whether you are clicking on the correct object
Answer by AndresFelipeMendez · Nov 14, 2018 at 02:21 PM
Hello im a bit puzzled by the function detectClick, who calls it? i made a small sample with a cube and a collider listening in OnMouseDown() and replacing theObject.name with gameObject.name but the rest remain the same and it works, my guess is that your method detectClick is never called.
detectClick is surely call as a callback of a button's onClick event.