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 GoodShepperd · Aug 05, 2013 at 01:08 AM · arrayarraysindexoutofrangeexception

IndexOutofRangeException

I have been receiving a rather confusing Array Index out of bounds exception. The following code shows where I initialize the array. The problem I am having is with the "players" array.

 var tilePrefab: GameObject;
 var unitPrefab: GameObject;
 var enemyPrefab: GameObject;
 var mapSize: int = 4;
 static var playerTurn: boolean;
 static var turnCount: int = 0;
 private var map: GameObject[,];
 var players: GameObject[];
 var playerCount: int =4;
 

 function Start () {
     
     createMap();
     playerCount = 4;
     players = new GameObject[4];
        //players = new GameObject[playerCount];
     generatePlayers();
     Debug.Log("hi " + players[0]);
 }

When I try to access the array during the following function I get an the out of bounds exception. However, if I try to access the array in the start function it works fine. the Debug.Log i put in the start function for testing works just fine without giving me any errors

 function moveCurrentUnit(tile: Tile)
 {
     var dest: Vector3; 
     dest = tile.getLocation();
     //players[turnCount].GetComponent(Unit).destination = dest;
     Debug.Log("hi " + players[0]);
     players[0].GetComponent(Unit).destination = dest;
     
     if (turnCount < playerCount - 1)
     {
         turnCount++;
     }
     else if (turnCount >= playerCount - 1)
     {
         turnCount = 0;
     }
 }

additionally when I use another function to access the map array it works fine as well

 function createMap()
 {
     map = new GameObject[mapSize, mapSize];    
     for(var i = 0; i < mapSize; i++)
     {
         for(var j = 0; j < mapSize; j++)
         {
             map[i,j] = newTile(i,j);
         }
     }
 }


I have no idea what could be causing this problem. What is the difference between the way I try to access map array and the players array other than the fact that map is 2 dimensional? I even tried making the players array 2 dimensional to see if that made any difference. I'm pretty stumped any help would be appreciated

Comment
Add comment · Show 2
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 robertbu · Aug 05, 2013 at 01:13 AM 0
Share

Is moveCurrentUnit called directly or indirectly by another component? If so, could it be calling the function before this Start() is called. What happens if you move the logic to Awake() ins$$anonymous$$d of Start()?

avatar image GoodShepperd · Aug 05, 2013 at 01:42 AM 0
Share

the $$anonymous$$oveCurrentUnit function is activated by a mouse click, The error only pops up when I click so I doubt that it could be getting called before the start function is. when i move the logic from start to awake I get an error at the same place, but ins$$anonymous$$d of only getting it when I click it happens every frame. from looking at it after that I realized that I am also accessing the players array in my update function, for some reason it only fails when I call moveCurrentUnit

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tw1st3d · Aug 05, 2013 at 01:12 AM

 private var map: GameObject[,];

fairly certain the , is causing it.

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 GoodShepperd · Aug 05, 2013 at 01:43 AM 0
Share

the problem I am having is with the players array, not the map array

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

15 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

Related Questions

Index is Less Than Zero + Flawed Code 1 Answer

Array index is out of range 0 Answers

IndexOutOfRangeException error for array points in range 1 Answer

my QandA array is not working when you choose 3 wrong answers 1 Answer

Array empties values on RunTime? 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