Hello! I am having a problem with my game. I have a script ( that is listed below ) that is supposed to show a text for some time and then make it disappear. This script is being done through a game object with a trigger collider. This script is working fine with the first game object, but then it is not working for the next game instance where I’ve used it ( as seen in the video listed below ). May anybody tell me what’s wrong in this instance and help me to fix this? Thank you!
Video link: zwjism
Script used for the first text trigger ( the attack text ):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class CollisionText : MonoBehaviour
{
public TextMeshProUGUI enemyWarning;
public float duration = 4f;
[SerializeField] private Animator anim;
void Start()
{
enemyWarning.enabled = false;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
StartCoroutine ( showText(other) );
}
}
IEnumerator showText(Collider2D player)
{
enemyWarning.enabled = true; // displays text
attackDemonstration();
yield return new WaitForSeconds(duration); // waits an amount of time x
enemyWarning.enabled = false; // makes the text invisible
Destroy(gameObject); // destroys the trigger after the x amount of time
}
void attackDemonstration()
{
for(int i=1; i<=3; i++)
{
anim.SetTrigger("Attack");
}
}
}
Script used for the second text trigger that doesn’t disappear ( the test text ):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class test : MonoBehaviour
{
public TextMeshProUGUI enemyWarning;
public float duration = 4f;
[SerializeField] private Animator anim;
void Start()
{
enemyWarning.enabled = false;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
StartCoroutine(showText(other));
}
}
IEnumerator showText(Collider2D player)
{
enemyWarning.enabled = true; // displays text
yield return new WaitForSeconds(duration); // waits an amount of time x
enemyWarning.enabled = false; // makes the text invisible
Destroy(gameObject); // destroys the trigger after the x amount of time
}
}