- Home /
How to make animation work on UI button click ?
Hello there,
I'm extremely new and bad at C#. I have a script that flawlessly works on a space click. What it is, is just a simple fade transition. How to make the transition happen even on button click ? This is the code. How to apply it so it works with a button click. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneChange : MonoBehaviour {
public Animator animator;
private int levelToLoad;
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
Scenechanger(1);
}
}
public void Scenechanger (int levelIndex)
{
levelToLoad = levelIndex;
animator.SetTrigger("Fade");
}
public void scenechangercomplete()
{
SceneManager.LoadScene(levelToLoad);
}
}
Comment
Do you mean UI button? So you can click it with your mouse?
Best Answer
Answer by Ellie97 · Oct 31, 2018 at 12:06 PM
Select your UI button in the inspector and add an OnClick() event. In this event, drag the object holding this script into the game object box and then in the drop down menu select the script and your Scenechanger function
Your answer
![](https://koobas.hobune.stream/wayback/20220612181246im_/https://answers.unity.com/themes/thub/images/avi.jpg)