hey guys… I’m following a tutorial on how to create a main menu and he used prefabs to animate buttons.
I’m trying to add events to the buttons using the onClick() in unity editor. the problem is that it seems there’s a problem when linking the prefab animation to the button . for example when I’m selecting the “new game” option but click on the continue button using the mouse, it triggers the continue onClick() events . I tried to add a condition so that the onClick() only triggers when I’m both selecting and pressing the button but it didn’t work… any idea how I can fix this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MenuButton : MonoBehaviour
{
[SerializeField] MenuButtonController menuButtonController;
[SerializeField] Animator animator;
[SerializeField] AnimatorFunctions animatorFunctions;
[SerializeField] int thisIndex;
[SerializeField] Button button;
[SerializeField] bool isPressedAnimationPlaying = false;
// Update is called once per frame
void Update()
{
if (menuButtonController.index == thisIndex)
{
animator.SetBool("selected", true);
if (Input.GetAxis("Submit") == 1 || Input.GetMouseButtonDown(0))
{
animator.SetBool("pressed", true);
isPressedAnimationPlaying = true; // Set the variable to true when the animation starts
}
else if (animator.GetBool("pressed"))
{
animator.SetBool("pressed", false);
animatorFunctions.disableOnce = true;
isPressedAnimationPlaying = false; // Set the variable to false when the animation ends
}
}
else
{
animator.SetBool("selected", false);
}
if (menuButtonController.index == thisIndex && animator.GetBool("pressed") && !isPressedAnimationPlaying)
{
button.onClick.Invoke();
}
}
}