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 01:21 PM · c#random

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;
     }
 }
Comment
Add comment
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

1 Reply

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

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.

Comment
Add comment · Show 3 · 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
avatar image mmangual_83 · Dec 31, 2013 at 01:53 PM 0
Share

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

avatar image sparkzbarca · Dec 31, 2013 at 02:04 PM 0
Share

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.

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

@sparkzbarca Got it working now Thanks!!! Full points and happy new year!!!

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

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


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