- Home /
The question is answered, right answer was accepted
Array Out of Index (When it Really is not??)
I am making a space game and I thought it would be cool to have a bunch of squares make up a bar that makes up your velocity bar (a bar that shows your velocity lerped between 0 and max velocity). But it has been quite a terrible experience so far.
I have my code, right? I don't change anything in the inspector including anything in the arrays except for assigning stuff to them, no length changes or anything. I know that arrays start at 0 and have it be from 12 however I check that the subtractor (or whatever) is always not equal to 0. SO... THERE IS ABSOLUTELY NO REASON FOR IT TO BE OUT OF RANGE.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class VelocityBar : MonoBehaviour
{
public Image[] forwardVelBar = new Image[12];
public Image[] reverseVelBar = new Image[3];
[SerializeField] int barsToIlluminate;
public int barsNotToIlluminate;
float reverseBars;
GameObject Player;
float playerVel;
// Start is called before the first frame update
void Start()
{
Player = GameObject.FindGameObjectWithTag("Player");
Debug.Log("is this running?");
}
// Update is called once per frame
void Update()
{
playerVel = Player.GetComponent<SpaceShip>().forwardVel;
barsToIlluminate = (int)((playerVel * 0.3f)*100);
for (int i = 0; i < barsToIlluminate; i++)
{
forwardVelBar[i].color = new Color(forwardVelBar[i].color.r, forwardVelBar[i].color.b, forwardVelBar[i].color.g, 255);
}
barsNotToIlluminate = 12 - barsToIlluminate;
if(barsNotToIlluminate >= 1)
{
for (int i = 0; i < barsNotToIlluminate; i++)
{
forwardVelBar[12-i].color =
new Color(forwardVelBar[12-i].color.r,
forwardVelBar[12-i].color.b, forwardVelBar[12-i].color.g, 85);
}
}
}
}
So I run it and it looks good, at first. The frame after it starts it says array is out of index. However, it literally did its job correctly. Like, bruh just do that again. BUT NO. Attached is a screenshot of the error and the success of a dramatic first frame (Also the script in the inspector).
So my starting velocity is 0.2 and the max is 0.4 so it looks good, six illuminated, six not. So I have been staring at Visual Studio for 10 hours about to eat my own eyes.
All I need in life right now is to figure out how to not have a false (I think) out of index error
Answer by logicandchaos · Jan 04, 2020 at 04:37 AM
if i=0 then 12-0 = 12 indexing forwardVelBar[12] which is out of bounds. You could start i at 1 or make it 11-i instead of 12-i.
No errors, yay! However, for some reason now only the sixth one from the top (A$$anonymous$$A the last one in the image) is not illu$$anonymous$$ated. So only one is illu$$anonymous$$ated which is sort of wack.
@Dragondean your original question has been answered. Post a new question if you want help with another issue.
This is probably caused by your 6th square not being inside the array.
Your overall approach is bad. First of all this line:
barsToIllu$$anonymous$$ate = (int)((playerVel * 0.3f)*100);
could easily create an index out of bounds. At no point do you ensure it stays within 0 - 11. Since you essentially multiply your velocity by 30 your max velocity would be 0.36666666. As soon as your velocity gets larger than 0.4 this expression would return 12 which is out of bounds. I don't really see the point of the "0.3" or the "100".
Since you just want to "illu$$anonymous$$ate" bars depending on your velocity it's much saver and simply to just use one loop that always iterates over all your images and just figure out which should be on / off in the loop
barsToIllu$$anonymous$$ate = (int)((playerVel * 0.3f)*100);
for (int i = 0; i < forwardVelBar.Length; i++)
{
Color col = forwardVelBar[i].color;
col.a = 0.333333f;
if(i > barsToIllu$$anonymous$$ate)
col.a = 1f;
forwardVelBar[i].color = col;
}
Also note that you use a Color value. The components of a Color is between 0 and 1, not between 0 and 255. That would be a Color32.
Follow this Question
Related Questions
Cant fetch components from GameObjects stored in an array? 0 Answers
Help with editing array from inside functions for PID controller (C#)[Fixed] 1 Answer
How do I fix this problem-it won't let me start my game in Unity because of an override problem 1 Answer
Script Error 1 Answer
IndexOutOfRangeException: Array index is out of range 2 Answers