- Home /
Random message generator out of control
I got this class where I have to generate a random message but for some reason it is going through all the messages at once rather fast and I don't know how to control it. Can anyone help me get it to where it displays a random message once every time I run the application? Thank you in advance and happy new year.
Here is the class I am working on:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class myFirstClass : MonoBehaviour {
public enum taskList { task1, task2, task3, task4, task5, task6 }
public static taskList currentTask = taskList.task1;
List<int> taskCounter;
public Rect GUIRectWindow;
public Rect r_msg, btn_Begin;
void Awake()
{
taskCounter = new List<int>(Enumerable.Range(1,3));
}
void OnGUI()
{
InstructionsWindow();
}
/// <summary>
/// Displays the instructions
/// </summary>
public void InstructionsWindow()
{
// Make a background box
GUI.Box(GUIRectWindow, "");
switch (currentTask)
{
case taskList.task1:
if (taskCounter.Count > 0)
{
int randomIndex = Random.Range(0, taskCounter.Count);
int index = taskCounter[randomIndex];
switch (randomIndex)
{
case 0:
drawLabel(r_msg, "Move the object left to right using a stick. Click 'Begin' when ready");
break;
case 1:
drawLabel(r_msg, "Move the object left to right using pliers. Click 'Begin' when ready");
break;
case 2:
drawLabel(r_msg, "Move the object left to right using a chicken. Click 'Begin' when ready");
break;
}
}
break;
}
if (drawButon(btn_Begin, "Begin"))
{
//TODO: When this button is pressed, add logic here
}
}
void drawLabel(Rect rect, string labelname)
{
GUI.Label(new Rect(GUIRectWindow.x + rect.x, GUIRectWindow.y + rect.y, rect.width, rect.height), labelname);
}
bool drawButon(Rect rect, string buttonName)
{
if (GUI.Button(new Rect(GUIRectWindow.x + rect.x, GUIRectWindow.y + rect.y, rect.width, rect.height), buttonName))
{
return true;
}
return false;
}
}
Answer by sparkzbarca · Dec 31, 2013 at 01:44 PM
OnGUI runs every frame
so its calling the box every frame
its an easy fix though
Bool MessageDisplayed;
void Start()
{
MessageDisplayed = false;
}
OnGUI
{
if(!MessageDisplayed)
InstructionsWindow();
}
Dont forget in your instructions window to at somepoint set it back to true;
Really though that means 24/7 youll have the box up (if you set it to true the moemnt you close the box, then the moment it closes it'll open again)
so you need to set up probably 2 bools
one for wether it should display and another for if it is.
so
if( DisplayTrigger && !MessageDisplayed)
DisplayInstructions();
then you can change messagedisplayed by starting it as false, changing to true when you draw the instructions and false when they click the button to exit them
and change display trigger so it for example only shows up when they press the help key or enter a trigger zone and it displays text to help you navigate that area of the world.
@sparkzbarca thank you for your reply, but could you probably explain the last part to me in pseudocode? Because I had a little trouble following you after the first code snippet. If its not too much trouble.
all i'm saying is that you need to set up and store conditions for when the message should be displayed
so first you need to answer the question. When do I want my message to be displayed.
Then you need to code to check for that and if it's true you need to set a bool variable to true so you register that hey this thing happened so we need to now TRY to show the message.
The next thing you need to do is make sure it isn't already displayed so whenever the message displays you need a bool that you set to true to acknowledge the message was displayed and then set it to false when it stops being set.
so in this case you'd have
Bool TriggerInstruction$$anonymous$$essage;
Bool IsInstruction$$anonymous$$essageDisplayed;
then
if(TriggerInstruction$$anonymous$$essage && !IsInstruction$$anonymous$$essageDisplayed)
//Display instructions
that wll make it so you check and say
if the trigger event that makes me want to display instrctuions happened AND there not currently displayed already
then display them.
@sparkzbarca Got it working now Thanks!!! Full points and happy new year!!!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Making a camera list 1 Answer
Unity random vs C# random 2 Answers
my random task generator not working 1 Answer
Make a question and answer window 0 Answers