Triggered Coroutine does not work correctly
Hi,
i have a little trigger and coroutine understanding
I created 2 triggers (the red pots). Each trigger should display text with a UI (text appears at the bottom left). As soon as the player touches the trigger, the UI text should be activated and Coroutine showText should then be started.
The Coroutine show text includes the following code
set GameObject Text to true
show mytext
wait 4 seconds
set GameObject Text back to false
set UI Text mytext to false
So now to my problem. If I now add 2 triggers with the same script, each with its own text, the coroutine starts to behave strangely If I run through trigger 1, the text appears, if I run directly to trigger 2, the text changes to trigger 2 text, but the time of the display is not retrieved correctly. If I stand in the middle of the trigger and both trigger permanently, it may be that some text is only displayed for 1 second or not at all.
What can that be?
ShowText Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShowText : MonoBehaviour
{
public Text mytext;
public string message;
public GameObject canvas;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
mytext.enabled = true;
StartCoroutine(wait());
}
}
IEnumerator wait()
{
canvas.SetActive(true);
mytext.text = message;
yield return new WaitForSeconds(4F);
canvas.SetActive(false);
mytext.enabled = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612234023im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Execute on one frame using a float - Been looking for a solution for a while now... 1 Answer
Gameobject doesnt move after build 0 Answers
How to detect the vertices of water4 using a trigger? 0 Answers
2D Power up script not working 0 Answers
How to explode an object when it touches a trigger? 1 Answer