R
R
RZC2021-04-13 22:42:41
Unity
RZC, 2021-04-13 22:42:41

How to start animation on button click?

Good day. I am new to Unity and Sharps. Faced such a problem:
There is a menu:
6075f2a323782067847953.png
On the left is the start button. When you click on it, the animation of the sun should work. The sun increases and covers the canvas completely.
The essence of the problem:
Animation works automatically:
6075f3c326f32009084286.png
I wrote the following code (the code was rewritten based on several guides a couple of times, so I apologize for the buggy code):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class PlayGame : MonoBehaviour, IPointerClickHandler
{
    public Animator SUN;
    public void OnPointerClick(PointerEventData eventData)
    {
        SUN = GetComponent<Animator>();
        if (Input.GetButton("Fire1"))
        {
            SUN.Play("SUN", 1);
        }

    }


// Start is called before the first frame update
void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

There may be an error in the components of the unit, so here are the screenshots:
6075f3709e6f9092719243.png
6075f38e6dad3548030341.png

The following comes out of the errors:
6075f3de5984a229937841.png

I hope for your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RZC, 2021-04-15
@RZC

Dance Macabre Solved the issue by fixing the code and adding a boolean variable in the animator. Thanks for your advice.

C
Crebor, 2021-04-14
@Crebor

Most likely the problem is in the animator, the error refers to this. Connections and transitions are not configured. Watch a short video - it should help.
https://www.youtube.com/watch?v=Ux6wQQIHimQ

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question