When I run this code Unity crashes(ANIMATOR RELATED)
So when I interact with this script in the game via testing, the entire unity window crashes with no explanation.. I'm not expert on code, so I would like some insight on this if at all possible
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPCSpeechBubbleColliderB : MonoBehaviour
{
[HideInInspector] private bool SpeechBubbleActive;
[HideInInspector] private bool PlayerInRange;
[SerializeField] private Animator Animator;
IEnumerator ON()
{
if (Input.GetKeyDown(KeyCode.E))
{
if (SpeechBubbleActive)
{
Animator.Play("Absent");
SpeechBubbleActive = false;
Update();
}
else
{
SpeechBubbleActive = true;
Animator.Play("Appear");
yield return new WaitForSeconds(4.00f);
Animator.Play("Absent");
SpeechBubbleActive = false;
Update();
}
}
}
private void Update()
{
if (PlayerInRange)
{
if (Input.GetKeyDown(KeyCode.E))
{
if (SpeechBubbleActive)
{
Animator.SetInteger("On", 0);
SpeechBubbleActive = false;
}
else
{
Animator.SetInteger("On", 1);
SpeechBubbleActive = true;
StartCoroutine(ON());
}
}
}
else
{
SpeechBubbleActive = false;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
PlayerInRange = true;
}
}
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
PlayerInRange = true;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("Player"))
{
Animator.SetInteger("On", 0);
PlayerInRange = false;
SpeechBubbleActive = false;
}
}
}
Essentially what I'm trying to do is run an event that check the completion of an animation state on a different object that is executed on an E press when within the proximity of the collider on the game object attached to this script. The reason I have to outsource the animator from the original game object is because the animation itself scales the entire game object object from 0 in one of the animations (which simulates it being invisible to the player and gradually growing) which would also set any collider's scale on that object to 0.
Is there a way to do this without enums? It seems that this one only manages to crash the game