Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mmangual_83 · Dec 31, 2013 at 08:31 PM · c#listrandom

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: alt text

Happy New Year to All!!!

debugwindow.png (57.5 kB)
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HappyMoo · Dec 31, 2013 at 08:47 PM 0
Share

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?

avatar image mmangual_83 · Dec 31, 2013 at 09:00 PM 0
Share

@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:

  1. I have three labels, each one with a distinct text.

  2. when the scene loads, a random number generator picks a number relative to the max count in the int list.

  3. 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.

  4. 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.

  5. 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.

avatar image HappyMoo · Dec 31, 2013 at 09:08 PM 0
Share

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?

avatar image mmangual_83 · Dec 31, 2013 at 09:09 PM 0
Share

It starts visible, it should be random

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 
     }
 
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Random message generator out of control 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges