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 ViolentValence · Aug 10, 2014 at 02:25 PM · listindexoutofrangeexceptionautocomplete

Function throws 'argument out of range, parameter: index' error

Hi, I'm trying to include a clumsy autocomplete text feature in my game, and the function that should return the suggested words in a list produces the above error when I Debug.Log it.

 #pragma strict
 import System.Collections.Generic;
 public var stringToEdit : String = "";
 var verblist = new List.<String>();
 verblist.Add("look"); 
 verblist.Add("swim");
 verblist.Add("loom");
 verblist.Add("lecture");
 public var words = new List.<String>(); //just showing the initialization
 
 stringToEdit = GUI.TextField (Rect (90, 10, 200, 25), stringToEdit, 40);
 //in the OnGUI()
 
 function getWord(){
         
         for (var i : int = 0; i < verblist.Count; i++){
             var j = -1;
             var correct = 1;
             var enteredText = stringToEdit.length;
             while (correct == 1 && j++ < enteredText){
             if (verblist[i].Substring(j,1) != stringToEdit.Substring(j))
                 { 
                 correct = 0;
                 }
             }
             if (correct == 1) 
             {
             words.Add(verblist[i]);
         }
         }
         Debug.Log(words[0]);
         return words;
     }

It's clearly not adding to words, because I'm logging index 0. Any ideas why? Thanks in advance

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 rutter · Aug 10, 2014 at 07:51 AM 0
Share

This bit:

 verblist.Add("look"); 
 verblist.Add("swim");
 verblist.Add("loom");
 verblist.Add("lecture");

$$anonymous$$ove that into an Awake() or Start() function, so that it'll be executed once your object spawns.

Aside from pretty basic default values for your variables, you usually don't want (or can't have) code that roams wild without being inside a function.

I'm hesitant to let this through the moderation queue because it's a frequently asked question. It's not a bad question, we're just trying to cut down on the number of duplicate threads in this area of the site. If you have anything else to add, feel free to comment here; if not, I'll drop the question from the queue in a little while.

If you can't find help, here, you can always try the forums or look up some tutorials online.

avatar image ViolentValence · Aug 11, 2014 at 06:01 AM 0
Share

I appreciate it, and sorry that I may have missed the FAQ within. Please note, though, that the issue was not with populating 'verbslist' but 'words' in the match-er function. I was able to get it working, but the new hurdle is that the function is either being called twice, or doubling itself:

 function getWord(){
             
     for (var i : int = 0; i < verblist.Count; i++){
         var j = -1;
         var correct = 1;
     while (correct == 1 && ++j < stringToEdit.length){
 if (verblist[i].Substring(j,1) != stringToEdit.Substring(j,1))
                 { 
                 correct = 0;
                 }
             }
             if (correct == 1) 
             {
             words.Add(verblist[i]);
         matchCounter++;
         Debug.Log(matchCounter);
             
         }
         }
         Debug.Log(words[0]);
         return words;
     }

For instance, this logs '1 2 3 4 look' if 'lo' is in the textfield (i.e. it's matching 'look' and 'loom' twice, then logging the first match). I've tried calling it from a keypress and as a start, and still it doubles the matches.

avatar image Kiwasi · Aug 11, 2014 at 06:16 AM 0
Share

Don't send a while loop to do the job of a for loop.

You code will be much easier to read and debug if you use the proper loops in their proper places.

Also will work better if you tell us which line is throwing the error.

avatar image ViolentValence · Aug 11, 2014 at 07:15 AM 0
Share
 function getWord()
 {
      
 for (var i : int = 0; i < verblist.Count; i++)
    {
     var j = -1;
     var correct = 1;
 
     while (correct == 1 && ++j < stringToEdit.length)
        {
 
         if (verblist[i].Substring(j,1) != stringToEdit.Substring(j,1))
            {
            correct = 0;
             }
         }
     if (correct == 1)
         {
           words.Add(verblist[i]); //this is where the word is matched
           matchCounter++; //this is being triggered twice as much as it should
           Debug.Log(matchCounter);
      
         }
    } 
 Debug.Log(words[0]); //shows first match
 return words;
 }

Definitely would rather not nest a while in a for, but can I still write a for that checks character by character until it finds one != then stops?

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Cycling through a List with constantly changing element count. Running into problems D: 2 Answers

Arguement out of range exception on a lists index 3 Answers

Runtime IndexOutOfRangeException when testing on iPhone 1 Answer

List of type Button has size set to zero at runtime 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