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() 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewKeypadController : MonoBehaviour
{
public Animator dooranim;
private bool opened = false;
[Header("Unlock code")]
public int[] code;
[Header("Current input code")]
public int[] currentCode;
int currentIndex = 0;
[Header("Temp input controls")]
public int input;
public bool sendInput;
private void Start()
{
ResetCurrentCode();
}
private void Update()
{
if (sendInput)
{
sendInput = false;
AcceptInput(input);
}
}
public void AcceptInput(int value)
{
currentCode[currentIndex] = value;
currentIndex += 1;
if (currentIndex >= code.Length)
{
for (int i = 0; i < code.Length; i++)
{
if (code[i] != currentCode[i])
{
Fail();
return;
}
}
Success();
}
}
void Fail()
{
Debug.Log("incorrect code");
ResetCurrentCode();
}
void Success()
{
Debug.Log("correct code");
dooranim = GetComponent<Animator>();
dooranim.SetBool("Opened", true);
Debug.Log("well done:)");
ResetCurrentCode();
}
void ResetCurrentCode()
{
currentIndex = 0;
currentCode = new int[code.Length];
for (int i = 0; i < currentCode.Length; i++)
{
currentCode[i] = -1;
}
}
void Opendoor()
{
dooranim = GetComponent<Animator>();
opened = !opened;
dooranim.SetBool("Opened", !opened);
Debug.Log("well done:)");
}
}