I’m using this script on a cube to detect out of bound colliding and alerting user:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class OutOfBounds : MonoBehaviour
{
public string message;
void Start(){
gameObject.GetComponent<Renderer>().enabled = false;
}
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "Player") {
DialogueManager.ShowAlert(message);
}
}
}
This is working fine but its showing many alerts at once.
How can I make show just one alert for a while, like 2 seconds?
Use a timestamp.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PixelCrushers.DialogueSystem;
public class OutOfBounds : MonoBehaviour
{
public string message;
public float alertCooldown = 2f;
private float nextAlertTime = 0;
void Start(){
gameObject.GetComponent<Renderer>().enabled = false;
}
void OnCollisionEnter(Collision collision) {
float now = Time.time;
if (now < nextAlertTime) {
return;
}
nextAlertTime = now + alertCooldown;
if (collision.gameObject.tag == "Player") {
DialogueManager.ShowAlert(message);
}
}
}
Well, “many”? suggests your player is repeatedly entering the collision and triggering the message, which in itself suggests something else maybe wrong. However, to limit the error to only the 1 then, you would need to add code to ensure if 1 is already showing, then another is not.. Id suggest the dialogueManager would be the place to sort that, and have some form of AlertSingleMessage and have it keep track that its not a duplicate