- Home /
Arrays and indices issues
Disclaimer: Yes, I do know that this is a somewhat common error, but in the way I'm doing it, I haven't found a solution. I've had similar questions to this taken down, but as far as I know this error is unique. If this isn't unique, feel free to take it down but please refer me to where I should look to find a solution to this.
I've been having issues with arrays for a while now. To keep it short, I'm trying to make a function that places a room, randomly places blocks, then places enemies on those blocks. The program then chooses one of the enemies in the array to hold a key. In code, it's along the lines of this:Check if should place a block. If block placed, check if should place an enemy. If should, instantiate enemy and place it in an array with all other enemies in the room. Once the room is finished generating, choose random placed enemy in array and change their variable hasKey to true. I've been combing the forums for four hours, and all solutions are either out of date or don't apply to my situation. As of right now, I'm getting the "index was outside the bounds of the array" error, but I know it's because the length of the array is 0. What I don't know is how to change the length of the array based on how many enemies are in the room. Setting the value too low results in the above error, but setting it too high results in blank slots and the chance that no enemy is picked. Any ideas as to how to do this? I understand if it's not possible with my current method, but I would ask that you steer me as to how to do this. Hopefully this post doesn't come across as insensitive or redundant. Code:
void generateCombatRoom()
{
Instantiate(baseRoom, new Vector2(roomPosX, roomPosY), transform.rotation);
startPosY = 3 / 2f;
startPosX = roomPosX - 19 / 2f;
int i = 0;
for (int j = 0; j < 20; j++)
{
randomChanceA = Random.Range(0, 2);
if (randomChanceA == 0)
{
Instantiate(brick, new Vector2(startPosX, startPosY), transform.rotation);
randomChanceB = Random.Range(0, 4);
if (randomChanceB == 0)
{
startPosY++;
enemies[i] = Instantiate(walkerEnemy[Random.Range(0, walkerEnemy.Length)], new Vector2(startPosX, startPosY), transform.rotation) as GameObject; //Placing the enemy
Debug.Log("Incrementing");
i++;
startPosY--;
}
}
startPosX++;
}
int enemyWithKey = Random.Range(0, enemies.Length); //Picking an enemy to give a key
enemies[enemyWithKey].gameObject.GetComponent<EnemyScript>().dropsKey = true; //Giving it a key
startPosX += 4;
}
Any answers are appreciated, and thanks for hearing me out. Thanks, EpicCrownKing
Perhaps you could use the List class ins$$anonymous$$d of an array.
Lists have great support dynamic ranges. IE GameObject[] enemies
becomes List<GameObject> enemies
You can add elements via enemies.Add()
and query the number of elements via enemies.Count
.
Answer by hexagonius · Jul 27, 2019 at 09:26 AM
@Schmit is correct. There is only one question to answer that determines if you should use an array or a list:
Do I know how many items I want to to put into it?
If the answer is yes, use an array. If the answer is no, use a list. In your case, use a List and that's it.
To be more clear about your specific situation. the for loop does have a hardcoded amount of times it runs (20). Since an outcome of one iteration, even though not likely, is adding an enemy into that array, and that means there is a chance of adding enemies as many times as there is loop iterations. You would usually create the array with the same length as the loop count before hand.
Sounds good. I turned the array into a list to make it more easily expandable, such as if I ever wanted to mess with the amount of times it loops. I had to modify the code to make it work correctly, but now I'm getting some strange errors with the same line that's been giving me issues. It all works the same, but once the room is finished, I reset the list back to empty. Either I'm doing it wrong, or something broke, because I'm getting another(yay) index error. In my $$anonymous$$d, with how I think lists work, it should work correctly. New Code: void generateCombatRoom() { Instantiate(baseRoom, new Vector2(roomPosX, roomPosY), transform.rotation); startPosY = 3 / 2f; startPosX = roomPosX - 19 / 2f;
for (int j = 0; j < 20; j++)
{
randomChanceA = Random.Range(0, 2);
if (randomChanceA == 0)
{
Instantiate(brick, new Vector2(startPosX, startPosY), transform.rotation);
randomChanceB = Random.Range(0, 4);
if (randomChanceB == 0)
{
startPosY++;
enemies.Add((GameObject)Instantiate(walkerEnemy[Random.Range(0, walkerEnemy.Length)], new Vector2(startPosX, startPosY), transform.rotation));
startPosY--;
}
}
startPosX++;
}
int enemyWith$$anonymous$$ey = Random.Range(0, enemies.Count); //Picking an enemy to give a key
enemies[enemyWith$$anonymous$$ey].gameObject.GetComponent<EnemyScript>().drops$$anonymous$$ey = true;
//The line above( enemies[enemyWith$$anonymous$$ey].....) is giving the error
enemies = new List<GameObject>();
startPosX += 4;
}
Any ideas as to how to fix the index error this time? Thanks for the response, EpicCrown$$anonymous$$ing
It's also a good idea to wrap this logic in a statement which verifies we have at least one item in our enemies array. IE
if (enemies.Count > 0)
{
int enemyWith$$anonymous$$ey = Random.Range(0, enemies.Count); //Picking an enemy to give a key
enemies[enemyWith$$anonymous$$ey].gameObject.GetComponent<EnemyScript>().drops$$anonymous$$ey = true;
}
max is exclusive for Random.Range(int $$anonymous$$, int max). Random.Range(0, enemies.Count)
is fine.
Your answer
Follow this Question
Related Questions
Array index is out of range 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Algorithm for loading sounds 1 Answer
new Mesh() works outside of array, but not in array. Huh? 3 Answers