using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spin : MonoBehaviour
{
[Range(1, 500)]
public float[] speeds;
public bool randomSpeed = false;
private GameObject[] objectsToSpin;
private bool randomSpeedInUpdate = true;
// Start is called before the first frame update
public void Init()
{
objectsToSpin = GameObject.FindGameObjectsWithTag("ClonedObj");
if (objectsToSpin.Length > 0)
{
speeds = new float[objectsToSpin.Length];
for (int i = 0; i < objectsToSpin.Length; i++)
{
if (randomSpeed == true)
{
speeds[i] = Random.Range(1, 500);
}
else
{
speeds[i] = 100;
}
}
}
}
// Update is called once per frame
void Update()
{
if (objectsToSpin.Length > 0)
{
for (int i = 0; i < objectsToSpin.Length; i++)
{
objectsToSpin[i].transform.Rotate(Vector3.down, speeds[i] * Time.deltaTime);
if (randomSpeed == true && randomSpeedInUpdate == true)
{
speeds[i] = Random.Range(1, 500);
randomSpeedInUpdate = false;
}
else
{
speeds[i] = 100;
randomSpeedInUpdate = true;
}
}
}
}
}
I tried to use a helper flag variable name randomSpeedInUpdate but still when I'm changing the randomSpeed flag to true while the game is running it keep changing the speeds all the time. I want it will change the speeds to random only once when changing it to true.