Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Octillerysnacker · May 10, 2017 at 02:17 AM · 2derrorlistloop

ArgumentOutOfRangeException: Argument is out of range error when used with for loop

In one one of my scripts, there is a 2D array that is iterated through every FixedUpdate(). Another script creates the nested elements. Every time though, I get an error that says ArgumentOutOfRangeException: Argument is out of range. Parameter name: index.

I've confirmed over and over again that the lists inside the lists, and the objects inside those nested lists exists through Debug.Log(). I can't figure out why the error is being thrown.

Here's the important code. The line where the error occurs is labeled:

FighterBehaviour.cs:

 public class Neuron
     {
         public List<float> weights = new List<float>();
         public float calculate(List<float> inputs)
         {
             float temp = 0;
             for(int i = 0; i < inputs.Count; i++)
             {
                 temp += inputs[i] * weights[i];
             }
             return temp;
         }
         public void generateWeights(float min, float max, int amount)
         {
             weights = new List<float>();
             for (int i = 0; i < amount; i++)
             {
                 weights.Add(Random.Range(min, max));
             }
         }
     }
     public void generateNeurons(int[] neurons)
     {
         for(int i = 0; i < neurons.Length; i++)
         {
             network.Add(new List<Neuron>());
             for(int j = 0; j < neurons[i]; j++)
             {
                 network[i].Add(new Neuron());
             }
         }
     }
 public List<List<Neuron>> network = new List<List<Neuron>>();
 
 void FixedUpdate () {
         List<float> inputs = new List<float>(4) { shotSeen, enemySeen, shotFired, FOVSize };
         List<float> outputs = new List<float>();
         for(int i = 0; i < network[0].Count;i++)
         {
             outputs.Add(network[0][i].calculate(inputs));
         }
         for(int i = 1; i < network.Count; i++)
         {
             List<float> temp2 = new List<float>();
             for(int j = 0; j < network[i].Count; i++)//This is the line the error always occurs
             {
                 temp2.Add(network[i][j].calculate(outputs));
             }
             outputs = new List<float>();
             for(int j = 0; j < temp2.Count; j++)
             {
                 outputs.Add(temp2[j]);
             }
         }
 }

SimulationManager.cs: public int population = 12; public float weightRange = 2f; public int[] networkStructure;

 void Start () {
         bots = new FighterBehaviour[population];
         for(int botIter = 0; botIter < population; botIter++)
         {
             bots[botIter] = Instantiate(bot);
             bots[botIter].gameObject.SetActive(false);
             bots[botIter].network = new List<List<FighterBehaviour.Neuron>>();
             bots[botIter].generateNeurons(networkStructure);
             for (int layerIter = 0; layerIter < networkStructure.Length; layerIter++)
             {
                 for(int neuronIter = 0; neuronIter < networkStructure[layerIter]; neuronIter++)
                 {
                     if(layerIter == 0)
                     {
                         bots[botIter].network[layerIter][neuronIter].generateWeights(-weightRange, weightRange, 4);
                     }else
                     {
                         bots[botIter].network[layerIter][neuronIter].generateWeights(-weightRange, weightRange, networkStructure[layerIter-1]);
                     }
                 }
             }
         }
     }
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
0
Best Answer

Answer by Jawchewa · May 10, 2017 at 03:54 AM

I'm not entirely sure how your code is working, but are you sure that the line with the error is supposed to be incrementing i instead of j? Are you sure on line with the error that you shouldn't be replacing i++ with j++? So something like this instead:

 for(int j = 0; j < network[i].Count; j++)

I could be wrong about what you are doing, but that seems like it would make more sense with this kind of loop.

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 Octillerysnacker · May 10, 2017 at 05:16 PM 0
Share

Of course it was going to be a simple typo, with two characters that look extremely similar. Thanks.

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

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

Related Questions

Error when adding an item to a list 0 Answers

Failed Re pack resources Unity Android 0 Answers

,"For" loop looping only once through my array 0 Answers

What is the most effective way to structure Card Effects in a Single Player game? 1 Answer

How do I change the image of an SVG sprite? 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