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 RealMTG · Jun 21, 2014 at 04:47 PM · loopienumeratorwhile

Changing a string in a loop each second

Hi!

I want to make a "connecting box" where it shows that it is connecting. Just to make it a bit fancier, I want my three last dots to appear and disappear. I made this IEnumarator to make a while loop. I honestly don't know how to make loops with WaitForSeconds so if you could help me, it would be very appreciated!

My code:

 void Start () {
     StartCoroutine (ConnectingStringUpdater ());
 }
 
 IEnumerator ConnectingStringUpdater () {
         while (connecting == true) {
             connectingString = "Connecting";
             yield return new WaitForSeconds(1);
             connectingString = "Connecting.";
             yield return new WaitForSeconds(1);
             connectingString = "Connecting..";
             yield return new WaitForSeconds(1);
             connectingString = "Connecting...";
             yield return new WaitForSeconds(1);
         }
     }


Thanks

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by lukos · Jun 21, 2014 at 05:32 PM

Hi, Try this, is cleaner:

     public bool connecting;
     public string connectingString;
 
     void Start () 
     {
         connectingString = "Connecting";
         StartCoroutine (ConnectingStringUpdater ());
     }
     
     IEnumerator ConnectingStringUpdater () 
     {
 
         while (connecting == true) {
             if (connectingString.Substring(connectingString.Length-3) != "...") connectingString += ".";
             else connectingString = "Connecting";
             yield return new WaitForSeconds(1);
         }
     }
Comment
Add comment · Show 2 · 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 RealMTG · Jun 21, 2014 at 05:55 PM 0
Share

It doesn't work. Now it just says "Connecting"

avatar image lukos · Jun 23, 2014 at 02:22 PM 0
Share

is connecting bool assigned as true? $$anonymous$$ust work

avatar image
0
Wiki

Answer by angarg12 · Jun 23, 2014 at 02:11 PM

Your solution works, but it is ugly because you repeat a lot of code. It is good that you are trying to improve it!.

Whenever you find yourself repeating too much code, think if you can make it a loop. You an outer loop, but you can also make the inside into a loop. Also, it is possible that this solution has poor performance, although it is possible that the compiler optimization takes care of that.

You can put your labels in an array and then circularly walk the array. The resulting code would look something like this (notice that it is also good practice to put "magic numbers" in descriptive variables).

 string displayLabel;
 string[] connectingLabels;
 bool connecting = true;
 public static readonly float LABEL_UPDATE_DELAY = 1f;
 
 void Start () {
     connectingLabels = new string[4];
     connectingLabels[0] = "Connecting";
     connectingLabels[1] = "Connecting.";
     connectingLabels[2] = "Connecting..";
     connectingLabels[3] = "Connecting...";
     displayLabel = connectingLabels [0];
     StartCoroutine (ConnectingStringUpdater ());
 }
 
 void OnGUI () {
     GUILayout.Label(displayLabel);
 }
 
 IEnumerator ConnectingStringUpdater () {
     int currentIndex = 0;
     while (connecting == true) {
         displayLabel = connectingLabels [currentIndex];
         yield return new WaitForSeconds(LABEL_UPDATE_DELAY);
         currentIndex = (currentIndex+1)%connectingLabels.Length;
     }
 }

Of course this code itself has some issues. You are fiddling around with the array index, which isn't a very good idea. Also your intention doesn't seems too clear from the code.

Can you do better? Of course! Since you are walking an array circularly, you might as well use a data structure made for that. In the next example we suppose you have a class CircularList. There are many implementations available on the internet if you don't feel like making your own (and you shouldn't).

 Node displayLabel;
 CircularList connectingLabels;
 bool connecting = true;
 public static readonly float LABEL_UPDATE_DELAY = 1f;
 
 void Start () {
     connectingLabels = new CircularList();
     connectingLabels.add("Connecting");
     connectingLabels.add("Connecting.";
     connectingLabels.add("Connecting..");
     connectingLabels.add("Connecting...");
     displayLabel = connectingLabels.head();
     StartCoroutine (ConnectingStringUpdater ());
 }
 
 void OnGUI () {
     GUILayout.Label(displayLabel.value());
 }
 
 IEnumerator ConnectingStringUpdater () {
     while (connecting == true) {
         yield return new WaitForSeconds(LABEL_UPDATE_DELAY);
         displayLabel = displayLabel.next();
     }
 }

I haven't checked this code, but it should give you an idea on how to implement it with a CircularList. The advantages of this solution is that you don't have to care about indexes at all, and your intent is much clearer from the code.

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
avatar image
0

Answer by Jeff-Kesselman · Jun 23, 2014 at 02:20 PM

Of course you could do it even simpler...

 Node displayLabel;
 int dotCount=0;
 bool connecting = true;
 public static readonly float LABEL_UPDATE_DELAY = 1f;
 
 void Start () {
     displayLabel = "Connecting";
     StartCoroutine (ConnectingStringUpdater ());
 }
 
 void OnGUI () {
     GUILayout.Label(displayLabel.value());
 }
 
 IEnumerator ConnectingStringUpdater () {
     while (connecting == true) {
         yield return new WaitForSeconds(LABEL_UPDATE_DELAY);
        displayLabel = "Connecting";
        for(int i=0;i<dotCount%4;i++){
            displayLabel+=".";
        }
        dotCount++;
     }
 }
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 angarg12 · Jun 23, 2014 at 03:02 PM 0
Share

Well, the whole point of making the code like that is to make it clear, not obscure. Also works in the general case, if you want to cycle through different messages and not only add dots.

There is also the performance issue, concatenating strings is expensive, although as I said maybe the compiler optimizes it and isn't such a big deal.

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

23 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 avatar image avatar image avatar image avatar image

Related Questions

Using Loops 1 Answer

What part of my While Loop is causing the game to freeze? (C#) 2 Answers

while loop & array- turn on something via percentage 1 Answer

Problem with while loop in Coroutine 2 Answers

Breaking from a Loop 2 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