unity-game-enginebuttoncoroutinekeycodeanimator

Need a way to use button click functionality vs GetKeyDown in Coroutine


This animator script works, however, in place of the Keycode input inside the WHILE LOOP, I need to use a UI button for mobile, which I haven't been able to figure out. I found an answer about putting a wrapper around it to make method available to click event, but have no idea how that's supposed to work within update function. It took me a long time to get this far, being a newbie to unity AND c#, so if you could provide a detailed answer or suggestion, I can get my life back, please help if you can.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using SWS;

public class goat_question : MonoBehaviour
{
    private Animator anim;
    public GameObject player;
    public Text ResultText;
    public Text AnswerText;  
    public Text AnswerText2;
    public Button GoatButton;

  

    void Start()
    {
        anim = GetComponent<Animator>();
        Button btn = GoatButton.GetComponent<Button>();
        btn.onClick.AddListener(TaskOnClick);

    }

    void TaskOnClick()
    {
        Debug.Log("You have clicked the button!");
    }
   
    // Update is called once per frame
    void Update()

    {
        if (AnswerText.text.Equals(AnswerText2.text))

        {                                         

            StartCoroutine(GoatWalkPathCoroutine());        

        }

         IEnumerator GoatWalkPathCoroutine()
        {
            while (true)  
            {              

                    if (Input.GetKeyDown(KeyCode.K))  
                {
                    anim.Play("goat_hi_walk");
                    player.GetComponent<splineMove>().enabled = true;
                    yield return new WaitForSeconds(27);
                    anim.Play("goat_hi_licking");

                }
                yield return null;
            }

        }

    }

}

Solution

  • In a separate script for just the UI button, have a bool called isClicked or something, and when the button gets clicked set that to true. In this main script, you can reference the one you just made, and instead of the Input.GetKey, you can say, if(otherScript.isClicked).