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!!!