Button to change a scene
Hello everyone, I am trying to do a button on a Title Screen to start my game and change the scene to the level 1. I can't figure out what's wrong with my script. Here it is :
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Lvl1Changer : MonoBehaviour {
void OnMouseDown()
{
SceneManager.LoadScene ("_lvl1");
Debug.Log("Click");
}
}
The function doesn't run when I click on the button becuase there is no Debug Log on the console.
Thank you for helping me and taking time to answer me.
Answer by Falsorr · Jul 15, 2017 at 04:36 PM
I found the answer, here's what my code looks like : using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Lvl1Changer : MonoBehaviour {
public void OnClick()
{
SceneManager.LoadScene ("_lvl1");
}
}
Make sure your function is public and then on the editor, select your button. In the Inspector there is a component called "Button (script). There is a list called OnClick() where you can select what happens when you puch the button. I added an Object. I dragged and drop my button from Hierarchy to the slot called "none (object) and in function, I choosed my script "lvl1changer) and choosed OnClick() which is the name of my public Function