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\$ Commented Jan 13, 2019 at 21:51

1 Answer 1

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\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.