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 A9.Games · Sep 30, 2012 at 02:58 PM · aiexceptionout of range

Argument out of range

Hi guys. I was following this tutorial for AI (http://youtu.be/KD0kLCZNQlc) and I'm changing it into C#, but I got an Argument out of range exception so i wanted to know how could I fix it. This is the script:

public List cells = new List();

public List doorsToCells = new List();

public List inmediateCells = new List();

public bool testForCells = true;

public float waitToTestCells = 2;

public int stage = 1;

[HideInInspector]

public bool doorOpen = true;

void Awake () { doorOpen = true;

 GameObject[] celda = GameObject.FindGameObjectsWithTag("AIPathCell");

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

     cells.Add(celda[i]);

 doorsToCells.Capacity = cells.Capacity;

 testForCells = true;

 waitToTestCells = 2;

 stage = 1;

}

void Update () { if (testForCells && waitToTestCells <=0) {

     foreach (GameObject inmediateCell in inmediateCells)
     {

         for (int i = 0; i &lt;= cells.Count-1; i++)

         {

             if (cells[i] == inmediateCell)

                 doorsToCells[i] = 1;

         }

     }



     for (stage = 2; stage &lt;= cells.Count; stage++)

     {

         for (int i = 0; i &lt;= cells.Count-1; i++)

         {

             if (doorsToCells[i] == stage -1)

                 foreach (GameObject checkDoor in cells[i].GetComponent().doors)

                {
                     if (checkDoor != gameObject)

                     {

                         foreach (GameObject checkCell in checkDoor.GetComponent().inmediateCells)

                        {

                             for (int j = 0; j&lt;= cells.Count-1;j++)
                             {

                                 if (cells[j] == checkCell)

                                     doorsToCells[j] = stage;
                             }
                         }
                     }
                 }   
         }
     }

     testForCells = false;
 }
 waitToTestCells -= 1;
 }

Thanks! Sorry for my english, I'm spanish.

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 Jessespike · Sep 30, 2012 at 03:18 PM 0
Share

Replace your Awake() with this. Not sure if this helps, untested...

void Awake () {

  for (int i=0; i<cells.Count; i++) {

     doorsToCells.Add( cells[i] );  

  }

}

avatar image A9.Games · Sep 30, 2012 at 03:41 PM 0
Share

It didn't work cause cells in a GameObject list, i'll edit and put the whole script. Thanks, anyway.

avatar image Jessespike · Sep 30, 2012 at 05:19 PM 0
Share

yeah, I thought about that after posting. Ok, I pasted the script and it came up with a few errors. First ones are the lists need to know what type they are going to store. Something like this:

     public List<GameObject> cells = new List<GameObject>();    
     public List<int> doorsToCells = new List<int>();
     public List<GameObject> inmediateCells = new List<GameObject>();
 
 Next error I'm getting is:
 foreach (GameObject checkDoor in cells[i].GetComponent().doors)
 and
 foreach (GameObject checkCell in checkDoor.GetComponent().inmediateCells)
 
 

These lines need to change. you need to specify which component to get. I don't know how you have your scripts and components set up, but I guess they should look something like this (I made up "Room" and "ThisClass???" for example here):

  foreach (GameObject checkDoor in cells[i].GetComponent<Room>().doors)
 
 foreach (GameObject checkCell in checkDoor.GetComponent<ThisClass???>().inmediateCells)
                                
 

Also, is your posted code your whole script? Is it inside a class?

avatar image A9.Games · Sep 30, 2012 at 05:31 PM 0
Share

Oh, just realized about that. The type for the lists and the components deleted when i copied the script here. (I'm new here so i don't really know how to put here the scripts properly).

But the exception I'm getting is in the doorsToCells, cause when i look at it in the inspector it keeps saying that its size is 0 and it doesn't set the values i set in the script.

And yeah, the script is in a class. Thanks a lot.

avatar image Jessespike · Sep 30, 2012 at 05:58 PM 0
Share

In Awake, you made a typo I think. I don't use <= when starting from 0 usually.

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

to

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


But that didn't fix the doorsToOpen problem, so I replace capacity line.

 for(int i = 0; i < celda.Length; i++)
     cells.Add( celda[i] );
         
 //doorsToCells.Capacity = cells.Capacity;
 for(int i = 0; i < cells.Count; i++)
     doorsToCells.Add( 0 );
Show more comments

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

12 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

Related Questions

Argument out of range 0 Answers

"ArgumentException: get_time can only be called from the main thread." 1 Answer

need some help with ArgumentOutOfRangeException 1 Answer

Tennis game like Grand slam tennis 1 Answer

How do I stop an Enemy NavMeshAgent from intersecting the player model? 3 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