Reputation: 11329
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spin : MonoBehaviour
{
public GameObject prefabToRotate;
[Range(1, 100)]
public int numberOfObjects = 5;
[Range(1, 500)]
public float[] speeds;
public bool randomNumbersOfObjects = false;
public bool randomSpeed = false;
private List<GameObject> instantiatedObjects = new List<GameObject>();
// Start is called before the first frame update
void Start()
{
speeds = new float[numberOfObjects];
if(randomNumbersOfObjects == true)
{
numberOfObjects = Random.Range(1, 100);
}
if(randomSpeed == true)
{
for(int i = 0; i < speeds.Length; i++)
{
speeds[i] = Random.Range(1, 500);
}
}
for(int i = 0; i < numberOfObjects; i++)
{
GameObject go = Instantiate(prefabToRotate);
instantiatedObjects.Add(go);
}
}
// Update is called once per frame
void Update()
{
for (int i = 0; i < numberOfObjects; i++)
{
instantiatedObjects[i].transform.Rotate(Vector3.down, speeds[i] * Time.deltaTime);
}
}
}
And how can I get random numbers and random speeds from the Range sliders ? 1, 100 and 1, 500 ? I want also to be able to change this values of the sliders in the Update and it will update in real time while running the game the number of objects and the random speeds.
Upvotes: 0
Views: 49
Reputation: 807
You set the length of your 'speeds' array to 'numberOfObjects', then you change the value of 'numberOfObjects', but your 'speeds' array still equals the old value of 'numberOfObjects'. Try setting the length of 'speeds' after you assign a random value to 'numberOfObjects', like so
if (randomNumbersOfObjects == true)
{
numberOfObjects = Random.Range(1, 100);
}
speeds = new float[numberOfObjects];
Upvotes: 1