List resulting in out of range
So i'm developing kind of a rythym game where i have 5 gameobjects that work as the dots the player has to click, so i made a script to activate these in rythym but randomly and it ended up activating some of the dots more than once, so i need a script that check if they're able to be activated (in other words, if their cooldown of activation is over) and gather the ones that are able in a array so i can assign a random index and get only one of them to activate only once
Why does the "activeBlows[index].SetActive(true);" always get a Index out of range?
public IEnumerator Beat()
{
while (start)
{
yield return new WaitForSeconds(60 / BPM);
CheckActive();
beatNow = !beatNow;
}
}
void CheckActive()
{
List<GameObject> activeBlows = new List<GameObject>();
foreach(GameObject blow in blows)
{
if (blow.GetComponentInParent<BlowParent>().Able == true)
{
activeBlows.Add(blow);
}
else
{
activeBlows.Remove(blow);
}
}
int index = Random.Range(0, activeBlows.Count - 1);
activeBlows[index].SetActive(true);
activeBlows[index].GetComponentInParent<BlowParent>().StartCooldown();
}
The beatNow bool's only function is to determine an upbeat or a downbeat, has nothing to do with this part. I'm calling the dots "blows" and the "BlowParent" script is what defines if they're able or not
public class BlowParent : MonoBehaviour
{
public bool Able, countElapsed;
float elapsed;
public float cooldown;
void Update()
{
if (countElapsed)
{
elapsed += Time.deltaTime;
}
if(elapsed > cooldown && Able == false)
{
Able = true;
countElapsed = false;
}
}
public void StartCooldown()
{
Able = false;
countElapsed = true;
elapsed = 0;
}
}
Your answer
Follow this Question
Related Questions
Trying to program two buttons to appear when the player in my game dies 0 Answers
Remembering list of camera positions is not working 0 Answers
Find the closest Enemy that has a tag added in a list? 1 Answer
Getting the element that has the same element number on another list ? 2 Answers
I have trouble understanding arrays and enums. When and how? 0 Answers