• 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
1
Question by Squall009 · Nov 26, 2011 at 02:04 AM · guiarrays

Pausing a foreach loop until user input

Hi everybody, so what I'm trying to accomplish here is setting up a text display system for the npcs in my game. Basically I have an array of text strings that i am iterating through individually and trying to display them one at a time only continuing after the player has pressed a button. I have looked at the coruoutine and yield functions but most of them are for a specific amount of time not a bool or something else. Here is a blerp from my code but basically what I need is someway to pause the loop without exiting it making it wait till a bool or something other than time has changed so it can go to the next string in the array without returning to the beginning and displaying the same text over and over again :P . This is in C# so if anyone could help me out that would be awesome!

void Update () { CountTheTexts(); ChangeTextAsNeeded(); }

 void ChangeTextAsNeeded()
 {
     if (npcscript.StartTalking == true && message.Text == null)
     {
         foreach (string text in textinput)
         {
             message.EnterText(text);

             if (text == textinput[textCount])
             {
                 Debug.Log("we went through all responses");
                 npcscript.StartTalking = false;
             }
         }
     }
 }

 void CountTheTexts()
 {
     textCount = textinput.Length;
     textCount -= 1;
 }
Comment
Add comment · Show 2
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 jahroy · Nov 26, 2011 at 02:44 AM 0
Share

The solution you're after will not involve "pausing a for loop".

You just want to use a variable to keep track of which word the user is on.

When the user successfully accomplishes something, increase the value of that variable by one and move on to the next word.

You're not quite grasping the concept of for loops and Unity's input system at this point.

You basically want to think of the Update and OnGUI functions as for loops that execute forever. You want to wait until a button is clicked and then conditionally increase the current index in your array of words.

avatar image Squall009 · Nov 26, 2011 at 02:31 PM 0
Share

Ya for right now the system I'm implementing is only kind of a placeholder in the future I imagine that is something I would do. Ya I don't completely understand script yet(only started about a week ago lol) but i'll definitely keep that in $$anonymous$$d and thanks for your help!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Nov 26, 2011 at 03:10 AM

I didn't understand completely your script, but I suppose you want to stop after message.EnterText, wait until the user press some key, then continue the loop. If this is the case, you could do something like this:

bool running = false; // true when coroutine is running

void Update () { if (!running){ // avoid starting multiple coroutines CountTheTexts(); StartCoroutine(ChangeTextAsNeeded()); } }

IEnumerator ChangeTextAsNeeded() // <- make this function a coroutine { running = true; // signal that coroutine started if (npcscript.StartTalking == true && message.Text == null) { foreach (string text in textinput) { message.EnterText(text); yield return 0; // assure at least one update cycle inside the loop while (!Input.GetKeyDown("space")){ // wait for space key... yield return 0; // but let Unity breath till next update } // continue the loop if (text == textinput[textCount]) { Debug.Log("we went through all responses"); npcscript.StartTalking = false; } } } running = false; // signal the coroutine has ended } Notice that the flag running was added to the code to avoid multiple coroutines being started: once one coroutine starts, it sets running and no other instances will be started until this one has finished.

Comment
Add comment · Show 1 · 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 Squall009 · Nov 26, 2011 at 02:27 PM 0
Share

This is the closest that I was looking for and worked very well! This caused me a lot of stress because i'm still very new at program$$anonymous$$g but thanks again worked like a charm!

avatar image
0

Answer by jahroy · Nov 26, 2011 at 02:54 AM

Here's a simple script that loops through an array of Strings when a button is clicked.

It should be a good starting point.

/* an array of Strings - you can edit this in the Inspector */

var wordList : String [] = ["Funky", "Junky", "Spunky"];

/ rectangles for the GUI controls /

var buttonRect : Rect = Rect(100, 100, 100, 40); var boxRect : Rect = Rect(100, 140, 100, 40);

/ an int that represents the current String in the array /

var currentWord : int = 0;

function OnGUI() { / draw a button /

 if ( GUI.Button(buttonRect, "Next word") ) {

     /* go to the next word if button clicked */

     currentWord ++;     

     /* go to zero when we hit the end of the list */

     if ( currentWord &gt;= wordList.length ) {
         currentWord = 0;
     }
 }

 /* display the current word in a box */

 GUI.Box(boxRect, wordList[currentWord]);

}

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Remove Items and Item Tooltips 0 Answers

Using arrays with OnTriggerEnter 0 Answers

need to shorten my code but unsure of how 3 Answers

Setting GUI textures 1 Answer

Writing to GUI from an array 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