- Home /
my random task generator not working
I have this class that should handle outputting a list of random GUI labels onto the screen, one at a time and never at once! And it is supposed to keep doing that until it runs out of messages (labels) and then it jumps to the next scene.
What it is currently doing is generating two random numbers at the beginning, and staying in one of the cases (I'm using switch cases, you will see it in the code below) outputting the same message until the list runs out. At least it is closing the application when the list is empty, so I am grateful for that.
Here is the class:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
[ExecuteInEditMode]
public class taskHandler : MonoBehaviour {
public enum myTasks { task1, task2, task3, task4, task5, task6 }
public static myTasks currentTask = myTasks.task1;
public static List<int> taskCounter;
mySceneManager obj_taskWindow;
public Rect GUIRectWindow;
public Rect r_msg, btn_Begin;
public static int randomIndex, index;
void Awake()
{
obj_taskWindow = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<mySceneManager>();
taskCounter = new List<int>(Enumerable.Range(1,3));
OnLevelWasLoaded(0);
}
void OnGUI()
{
InstructionsWindow();
}
void OnLevelWasLoaded(int indx)
{
randomIndex = Random.Range(0, taskCounter.Count);
}
/// <summary>
/// Displays the instructions
/// </summary>
public void InstructionsWindow()
{
// Make a background box
GUI.Box(GUIRectWindow, "");
switch (currentTask)
{
case myTasks.task1:
switch (randomIndex)
{
case 0:
drawLabel(r_msg, "Move the object left to right using the Tap gesture. Click 'Begin' when ready");
break;
case 1:
drawLabel(r_msg, "Move the object left to right using the Grab gesture. Click 'Begin' when ready");
break;
case 2:
drawLabel(r_msg, "Move the object left to right using the Grip gesture. Click 'Begin' when ready");
break;
}
break;
}
if (drawButon(btn_Begin, "Begin"))
{
obj_taskWindow.currentWindow = mySceneManager.Windows.NextWindow;
}
}
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;
}
}
And here is the meths I am using to handle generating the random number once a button has been pressed.
if (drawButon(btn_Next, "Next"))
{
if (taskHandler.taskCounter.Count > 0) // should keep you in the first task until the condition is met
{
taskHandler.randomIndex = Random.Range(0, taskHandler.taskCounter.Count);
taskHandler.index = taskHandler.taskCounter[taskHandler.randomIndex];
taskHandler.taskCounter.RemoveAt(taskHandler.randomIndex);
mySceneManager.currentWindow = mySceneManager.Windows.Window2; //This goes back to the previous class
}
else
mySceneManager.currentWindow = mySceneManager.Windows.TaskComplete; // closes the application
}
Here is the debug window so it may shed even more light into this matter:
Happy New Year to All!!!
I don't get what you're trying to do exactly. Your code doesn't even include the Debug.Log code that is supposed to clear up what you're trying to do.
However.. I'm not sure I want to see the whole code - I would prefer some more details on what you exactly want to do and what you have Problems with.
Like, if your problem is only picking stuff out of a list in a random order without doubles or the ti$$anonymous$$g(not at once), let's just discuss that and not the part you already figured out, like actually showing the labels etc...
So... do the labels have to appear over time - like with one second between them? Do you just replace the text of one label or do you have to put new labels out and the old ones have to stay?
@Happy$$anonymous$$oo it is just the text that is supposed to change. The text only changes when the user cliks on the button.
Basically it is like this:
I have three labels, each one with a distinct text.
when the scene loads, a random number generator picks a number relative to the max count in the int list.
When the user clicks on the next button that text is removed from the list and then a new text appears (randomly picked) from the remaining two in the list.
When the user clicks on the next button that text is removed from the list and then a new text appears (randomly picked) from the remaining one in the list.
After the user clicks on the next button, it checks if the list is now empty, if it is then it finishes and the application closes.
You mix what you want to achieve and how you think is the best way to implement this I think.
Can you confirm that there is only one label visible at a time and after clicking the next button, you get one of the remaining texts shown. And it switches scenes after you have no more texts. Does it start with the first text shown or invisible?
Answer by HappyMoo · Dec 31, 2013 at 09:28 PM
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ShowMessages : MonoBehaviour {
public List<string> messages = new List<string>();
private string currentString = "";
void Awake () {
showNext();
}
// Shows next random message. returns false if ther was no more message
bool showNext()
{
if (messages.Count==0) return false;
int randomIndex = Random.Range(0, messages.Count);
currentString = messages[randomIndex];
messages.RemoveAt(randomIndex);
return true;
}
void OnGUI()
{
if (GUI.Button(new Rect(10,10,100,30), "Next"))
{
bool thereWasAnotherText = showNext ();
if (!thereWasAnotherText)
{
Debug.Log ("No more Text, Do something!");
}
}
GUI.Label(new Rect(10,110,200,30), currentString);
}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Printing a GUI selection grid in order 1 Answer
Printing an ordered list 1 Answer
Making a camera list 1 Answer