Hey !
Im coding message system. Idea is that i can add any message to my queue and system handles it’s drawing and clearing automaticly. I writed Message.cs class myself.
But when i add one message to the queue in void Start() method the message gets printed/cleared twice !
And i cannot figure out why ![]()
Please check out my code and suggest anything…
Here is the picture of print log and code:
using System.Collections.Generic;
using UnityEngine;
public class MessageManager : MonoBehaviour
{
public Queue<Message> MessageQueue = new Queue<Message>();
public Message ActiveMessage = new Message();
private bool m_showingMessage = false;
private string m_text;
private float m_timeLeft;
public void AddMessage(string value, float delay, bool isPriority = false)
{
if (isPriority == true)
{
Message message = new Message(value, delay, isPriority);
MessageQueue.Enqueue(message);
}
else
{
if (MessageQueue.Count > 10)
{
// trash message
}
else
{
Message message = new Message(value, delay, isPriority);
MessageQueue.Enqueue(message);
}
}
}
private void CheckMessageQueue()
{
if (MessageQueue.Count != 0 && m_showingMessage == false)
{
ActiveMessage = MessageQueue.Dequeue();
DrawMessage();
}
}
private void DrawMessage()
{
m_text = ActiveMessage.GetMessage();
m_timeLeft = ActiveMessage.GetDelay();
print(m_text);
m_showingMessage = true;
}
private void ClearMessage()
{
m_text = "";
m_showingMessage = false;
m_timeLeft = 1;
print("Message cleared!");
}
private void Update()
{
if (m_timeLeft < 0 && m_showingMessage == true)
{
if (m_text != "")
{
ClearMessage();
}
}
else
{
m_timeLeft -= Time.deltaTime;
CheckMessageQueue();
}
}
// Use this for initialization
private void Start()
{
AddMessage("Test !", 5, true);
}
}
Thank you in advance!