Search Unity

Question Animation won't play at all. Wondering how to fix

Discussion in 'Animation' started by DanielBrunt, Feb 6, 2021.

  1. DanielBrunt

    DanielBrunt

    Joined:
    Jan 17, 2021
    Posts:
    3
    Hey! I've been working on a puzzle game in Unity2D. All of the models in this game are 3D and I've been working out all of the code in a test level. I started making games just a couple weeks ago so I've been struggling with the animation factor -

    gameissue.JPG
    The green arrow at the bottom is a power up with two animations that I've already made. On shoots the arrow upwards and the other makes it float up and down. I want these animations to be triggered once the crate is pushed onto the button (the button being the red circle on the left). There is another animation where the yellow lock flies out of frame after the button is pushed down, but I was able to get that one to work fine. Here's the code:

    (Button Script)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ButtonMechanics : MonoBehaviour
    7. {
    8.  
    9.     public GameObject[] walls;
    10.     public Text wallAssecibilityCounter;
    11.  
    12.     void Start()
    13.     {
    14.         //This text is offscreen and is only used as a mechanism that I plan to use for different walls causing different things to happen
    15.         wallAssecibilityCounter.text = "0";
    16.     }
    17.     void OnTriggerEnter(Collider other)
    18.     {
    19.         if(other.tag == "Crate")
    20.         {
    21.             //The text changes (which activates the code on the arrow animation script)
    22.             wallAssecibilityCounter.text = "1";
    23.             //Waits for the lock animation to play
    24.             StartCoroutine(Wait(1.2f));
    25.             //This part is unrelated - in short, this allows you to pass a wall
    26.             foreach(GameObject wall in walls)
    27.             {
    28.                 Rigidbody wallNewRB = wall.AddComponent<Rigidbody>() as Rigidbody;
    29.             }
    30.             //Reverts the text back to 0
    31.             wallAssecibilityCounter.text = "0";
    32.         }
    33.     }
    34.     IEnumerator Wait(float duration)
    35.     {
    36.         yield return new WaitForSeconds(duration);
    37.     }
    38. }
    (Arrow Script)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ArrowAnimController : MonoBehaviour
    7. {
    8.     public Text wallAccessibilityText;
    9.     public Animator anim;
    10.     void Start()
    11.     {
    12.         //Gets the animator already on the arrow
    13.         anim = GetComponent<Animator>();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         //Checks if the button has been triggered
    19.         if(wallAccessibilityText.text == "1")
    20.         {
    21.             //Plays animation which is also connected to the second animation (see below)
    22.             anim.Play("PowerUpFlyUp");
    23.         }
    24.     }
    25. }
    This is the animation controller:

    upload_2021-2-6_16-16-37.png

    This is the inspector:

    upload_2021-2-6_16-18-34.png

    The arrow doesn't animate at all with these.
    Been trying to figure this out for hours...
    Help would be appreciated!
     

    Attached Files: