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().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).![alt text][1]
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
[1]: /storage/temp/150799-screenshot-29.png
↧