0
\$\begingroup\$
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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Try walking through your code one instruction at a time, exactly how the processor would do it. You'll find very quickly why it keeps recalculating the speeds - because that's exactly what you told it you wanted it to do. \$\endgroup\$
    – DMGregory
    Jan 13 '19 at 21:51
1
\$\begingroup\$

Rewrite your Update function to

void Update()
{
    if (randomSpeed && randomSpeedInUpdate)
    {
        for (int i = 0; i < speeds.Length; i++)
        {
            speeds[i] = Random.Range(1, 500);

            if (i == speeds.Length - 1)
                randomSpeedInUpdate = false;
        }
    }
    else if (!randomSpeed)
    {
        for (int i = 0; i < speeds.Length; i++)
        {
            speeds[i] = 100;
        }
        randomSpeedInUpdate = true;
    }

    for (int i = 0; i < objectsToSpin.Length; i++)
    {
        objectsToSpin[i].transform.Rotate(Vector3.down, speeds[i] * Time.deltaTime);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.