- Home /
For Loop is Only doing it's Job Once
[Not a Duplicate of my old question just looks similar because it is on the same concept]
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. However, every time I run this only one of the boxes is translucent (the top one AKA the first ran through the for loop), when the for loop should disilluminate 6 of them (since my start speed is one half of the max). Then if I accelerate to max speed the box gets filled in but then doesn't change after a deceleration.
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");
}
// 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 = 1; 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);
}
}
}
}
Attached is a Gif of the problem. The stuff in blue on the left is the bar.
So, I need to have this for loop actually turn off all of the little bars that it needs to rather than it just turning off the top one once and never turning it back on. I have been staring at Visual Studio for 10 hours about to eat my own eyes. Please, how do I fix this?
Answer by Bunny83 · Jan 04, 2020 at 09:47 PM
I actually answered this already in a comment to your old question. You use Color values. The components of the Color struct are float values and are in the range 0 - 1 and not 0 - 255. Therefore when you pass 255 or 85 in both cases you will saturate the value to "1.0" in both cases.
Note if you want to use "byte" values for each component, Unity has the Color32 struct. Its a seperate struct which has 4 byte values instead of float. A Color32 value can be converted to a Color value implicitly. So if you want to specify byte values you can just use a Color32 value instead. Here's my other example with a Color32:
for (int i = 0; i < forwardVelBar.Length; i++)
{
Color32 col = forwardVelBar[i].color;
col.a = 85;
if(i > barsToIlluminate)
col.a = 255;
forwardVelBar[i].color = col;
}
Answer by Iarus · Jan 04, 2020 at 08:56 PM
I can't find what's wrong with your code just by looking at it. But since you're using Visual Studio, have you tried debugging your code? Click on the "Attach to Unity" button. Put a breakpoint on the fist line of your Update method. The next time the VelocityBar.Update() method is called, the game will freeze and VisualStudio will take control. Now execute the code step by step, look at the buttons in the Debug toolbar, and inspect the value of all the variables at each step. Do the maths yourself on paper, follow the whole algorithm and validate that it's doing the right thing or not. To unfreeze the game, remove the breakpoint and click the resume or continue button.