Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Animation is not playing

Discussion in 'Animation' started by GiorgiNinua, Sep 18, 2023.

  1. GiorgiNinua

    GiorgiNinua

    Joined:
    Jul 5, 2023
    Posts:
    10
    Hello fellow unity devs
    I made a keypad system that works fine, however it is supposed to play an animation when code is correct but it doesn’t. I assigned the animator in inspector and even used a getcomponent<animator>() but the console still says that no animator is attached to the script while it tries to access it.
    Any idea how to fix it? Thanks in advance
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class NewKeypadController : MonoBehaviour
    6. {
    7.     public Animator dooranim;
    8.     private bool opened = false;
    9.  
    10.     [Header("Unlock code")]
    11.     public int[] code;
    12.  
    13.     [Header("Current input code")]
    14.     public int[] currentCode;
    15.  
    16.     int currentIndex = 0;
    17.  
    18.     [Header("Temp input controls")]
    19.     public int input;
    20.     public bool sendInput;
    21.  
    22.     private void Start()
    23.     {
    24.         ResetCurrentCode();
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         if (sendInput)
    30.         {
    31.             sendInput = false;
    32.             AcceptInput(input);
    33.         }
    34.     }
    35.  
    36.     public void AcceptInput(int value)
    37.     {
    38.         currentCode[currentIndex] = value;
    39.  
    40.         currentIndex += 1;
    41.  
    42.         if (currentIndex >= code.Length)
    43.         {
    44.             for (int i = 0; i < code.Length; i++)
    45.             {
    46.                 if (code[i] != currentCode[i])
    47.                 {
    48.                     Fail();
    49.                     return;
    50.                 }
    51.             }
    52.             Success();
    53.         }
    54.     }
    55.  
    56.     void Fail()
    57.     {
    58.         Debug.Log("incorrect code");
    59.         ResetCurrentCode();
    60.     }
    61.  
    62.     void Success()
    63.     {
    64.         Debug.Log("correct code");
    65.         dooranim = GetComponent<Animator>();
    66.         dooranim.SetBool("Opened", true);
    67.         Debug.Log("well done:)");
    68.         ResetCurrentCode();
    69.     }
    70.  
    71.  
    72.     void ResetCurrentCode()
    73.     {
    74.         currentIndex = 0;
    75.         currentCode = new int[code.Length];
    76.         for (int i = 0; i < currentCode.Length; i++)
    77.         {
    78.             currentCode[i] = -1;
    79.         }
    80.     }
    81.  
    82.     void Opendoor()
    83.     {
    84.         dooranim = GetComponent<Animator>();
    85.         opened = !opened;
    86.         dooranim.SetBool("Opened", !opened);
    87.         Debug.Log("well done:)");
    88.     }
    89. }
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    well if you watch the animator, is the boolean changing, is the animation state changing?
     
  3. GiorgiNinua

    GiorgiNinua

    Joined:
    Jul 5, 2023
    Posts:
    10
    No it isn’t. The animation doesn’t activate
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    ok. So as is the boolean changing on the animator? Have you confirmed your success code is running? These are basic steps to check
     
  5. GiorgiNinua

    GiorgiNinua

    Joined:
    Jul 5, 2023
    Posts:
    10
    nope, the boolean is not changing and i can confirm that success code is runing
     
  6. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    711
    then you need to work out why the boolean isnt changing as nothing will work till that does