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 AshwinSinha · Oct 20, 2013 at 05:21 PM · ienumeratorfor-loopindexof

For loop & Foreach don't complete inside IEnumerator when using IndexOf

Hi, I'm having a weird problem where a loop won't complete when I use IndexOf inside the loop.

Here's my code:

         for (int i = 0; i <= games.Length; i++)
         {
             
             if (games[i] != "")
             {
             
             iGUIButton button = scrollView2.addElement<iGUIButton>();
             button.type = iGUIButtonType.ButtonBigYellow;
             button.label.text = games[i].Substring(0,games[i].IndexOf('?'));
             button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length);
             button.clickCallback += play;    
             
             }
             
         }

Thanks!

Comment
Add comment · Show 6
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 Bunny83 · Oct 20, 2013 at 05:35 PM 1
Share

What does "not complete" mean? An infinite loop? in other words a crash? Does it ter$$anonymous$$ate too early? Do you actually talk about the loop or about the coroutine? Do you get any errors? If so you might want to post them here.

btw your code will produce an array-out-of-bounds error because the largest possible index into the "games" array is (games.Length-1) but you iterate until (games.Length)

avatar image AshwinSinha · Oct 20, 2013 at 06:08 PM 0
Share

Hi, Sorry, I forgot to mention that all values do contain a '?'. By not complete I mean that it runs once, some lines of the code don't execute at all. It runs perfectly if I remove the IndexOf and just use the strings. Edit : Also, I'm calling the StartCoroutine in void Start()

avatar image AshwinSinha · Oct 20, 2013 at 06:10 PM 0
Share

foreach (string game in games) { //code } does the same thing with the IndexOf in the code. Runs find when not doing any IndexOf operations.

avatar image ReVo_ · Oct 20, 2013 at 06:29 PM 0
Share

I think the problem is caused by index, with indexOf("?") + 1 you could go out of the string.. are you sure the "?" cant be found at the end of the string? Anyway try to edit this: button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length); to button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length - 1);

avatar image AshwinSinha · Oct 20, 2013 at 07:13 PM 0
Share

By design, there are always exactly 9 characters after the "?" so that's not the issue. I'm afraid

button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length - 1);

didn't help either. So the string I recieve is :

test2?testes562,test1?testtes102

yet,

string[] games = swww.text.Split (','); foreach (string game in games) { if (game != "") { Debug.Log(game.Substring(0,game.IndexOf('?'))); Debug.Log(game.Substring(game.IndexOf('?') + 1, game.Length)); }

Would only return

test2

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by ReVo_ · Oct 20, 2013 at 05:35 PM

EDIT3: ok i think i found the problem

If we have this string:

Hello World

If we start to extract from the o

Hell"o" (the o)

(Code:

             string hello = "Hello World";
 
             Console.WriteLine(hello.Substring(hello.IndexOf('o'), hello.Length));

)

and then we set length to hello.Length it except to extract hello.Length chars from startIndex index (hello.indexOf)

i think this can fix the problem, try:

 string.Length - indexOf('?')

I update this too in case someone have the same problem and dont see comments

EDIT2: Since in the comment it look horrible:

Edit

 button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length);

to

 button.variableName = games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length - 1);

Anyway i think indexOf('?') + 1 can be a problem, are you sure the '?' cant be found at the end of the string? If the ? is at the end it can go out of the string

EDIT: As bunny83 said, the

 for (int i = 0; i <= games.Length; i++)

is wrong because "i" will be games.Length and it will cause a index-out-of-bounds exception.

indexOf returns -1 if it failed the search (the string dont have the char).. maybe its the problem?

(I cant comment)

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 AshwinSinha · Oct 20, 2013 at 06:08 PM 0
Share

Hi, Thanks for answering, please refer to my comment on the question.

avatar image AshwinSinha · Oct 20, 2013 at 07:24 PM 0
Share

Didn't see the answer sorry, Commented again

avatar image
0

Answer by rutter · Oct 20, 2013 at 07:30 PM

As has been pointed out twice, now, your for loop is likely to cause an IndexOutOfRangeException.

In addition, your substring call:

 games[i].Substring(games[i].IndexOf('?') + 1, games[i].Length);

Is likely to cause an ArgumentOutOfRangeException. When calling string.Substring(), you need to make sure that startIndex plus length indicates a valid index in the string.

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

18 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

Related Questions

How to end a for loop that is inside of an IEnumerator and is being called from another script 3 Answers

Endless runner obstacles spawning problem 1 Answer

Delay after input? 1 Answer

IEnumerator Coroutine and update issues 2 Answers

How can I spawn different GameObjects using IEnumerator? 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