Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Why isn't this small animation working?

Discussion in 'Animation' started by Gummi_Ghost, May 30, 2022.

  1. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    Could I please get some help for this animation animation to play, and to play once? The animation for the button only works if the culling mode is set to 'Always activate". The code below is not attached to the button the animator plays on, but to a UI panel.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7. public class IntroLines : MonoBehaviour
    8. {
    9.    public TextMeshProUGUI textComponent;
    10.    public string[] BeginningLines;
    11.    public float textSpeed;
    12.    private int index;
    13.    public Button IntroButton4;
    14.  
    15.    public Animator anim4;
    16.  
    17.    void Start()
    18.    {        
    19.       Button btn4 = IntroButton4.GetComponent<Button>();
    20.          
    21.       btn4.onClick.AddListener(DisplayNextLine);
    22.  
    23.       anim4 = IntroButton4.GetComponent<Animator>();
    24.          
    25.       textComponent.text = string.Empty;
    26.       startIntroLines();
    27.  
    28.       anim4.Play("SlowButtonRise");
    29.    }
    30.  
    31.  
    32.    void DisplayNextLine()
    33.    {
    34.       if(textComponent.text == BeginningLines[index])
    35.          {
    36.             NextIntroLine();
    37.          }
    38.          else
    39.          {
    40.             StopAllCoroutines();
    41.             textComponent.text = BeginningLines[index];
    42.          }
    43.    }
    44.  
    45.  
    46.    void startIntroLines()
    47.    {
    48.       index = 0;
    49.       StartCoroutine(TypeLine());
    50.  
    51.      
    52.    }
    53.  
    54.    IEnumerator TypeLine()
    55.    {
    56.       foreach (char c in BeginningLines[index].ToCharArray())
    57.       {
    58.          textComponent.text += c;
    59.          yield return new WaitForSeconds (textSpeed);
    60.       }
    61.      
    62.       anim4.Play("SlowButtonRise");
    63.  
    64.    }
    65.  
    66.    void NextIntroLine()
    67.    {
    68.       if (index < BeginningLines.Length - 1)
    69.       {
    70.          index++;
    71.          textComponent.text = string.Empty;
    72.          StartCoroutine(TypeLine());
    73.       }
    74.       else
    75.       {
    76.          //gameObject.SetActive(false);
    77.       }
    78.    }
    79.  
    80.  
    81. }
    82.