Im currently trying to make it so a GUI button, when clicked/pressed, resets the scene. but i cant seem to get it to trigger the code im trying to get it load. could somebody help me?
using UnityEngine;
using System.Collections;
public class Reset : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void TaskOnClick() {
Application.LoadLevel(Application.loadedLevel);
}
}
I recommend you subscribe to onClick from code (not from Inspector):
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
[RequireComponent(typeof(Button))]
public class Reset : MonoBehaviour {
private Button button;
void Start() {
button = GetComponent<Button>();
button.onClick.AddListener(TaskOnClick);
}
void TaskOnClick() {
Application.LoadLevel(Application.loadedLevel);
}
}
Note that this script needs component Button on the same gameObject.
Line [RequireComponent(typeof(Button))] guaranties that when you add this component to gameObject, Unity also adds Button to it. But it does’n if component Reset is already on gameObject when you add this attribute.