- Home /
Button Canvas OnClick no LoadScene
I'm trying to follow these Unity 5 tutorials. But I've had some trouble. I Scripting a play button to Open a Scene when it gets clicked. In the video tutorial the button On Click space should look like this.
But in my Unity 5 Editor When I try to follow, Canvas has no Function LoadOnClick.LoadScene. And it looks like this .
Can someone please help?
In the tutorial they reference a script called something with Canvas. But you're referencing a gameobject. What's with the submenu under Canvas? Check the script for the function and is it exists and it's attached you the gameobject you've referenced you should find it under the according submenu.
LoadOnClick is likely a Component attached to the Canvas in the tutorial. Notice that the other Components on the Canvas are exposed on the dropdown menu. In the tutorial, did they create a class having that name? Perhaps you haven't attached that script to the Canvas? If you have attached that Script to the Canvas, the class may not have public access so that it can be exposed to the Reflection that drives the addition of elements you see in the bottom screenshot to the dropdown. Simply give the class public access by changing its declaration from class LoadOnClick...
to public class LoadOnClick
.
In the tutorial we never made a made any classes. The only thing we did to the Canvas was make it. There's a person at this question "http://answers.unity3d.com/questions/953437/button-onclick-load-scene.html" with the same problem.
Answer by ooblii · Sep 14, 2017 at 10:36 PM
create a new script called SceneLoader.cs and attach it to the canvas. Paste this in:
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour {
public void LoadSceneOnClick(int sceneNo)
{
SceneManager.LoadScene(sceneNo);
}
}
Now drag the canvas back into the OnClick listener for your button and select the LoadSceneOnClick(int) method of SceneLoader from the drop down and assign it the integer of the scene you want to load. Remember, scene numbers are zero indexed.