Quantcast
Channel: Answers for "Random cubes"
Viewing all articles
Browse latest Browse all 4

Answer by Xarbrough

$
0
0
Don't use 25 scripts. Use 1 script to manage all your objects, then just put them in a collection (array or list) to reference them. Try this: using UnityEngine; using System.Collections; using System.Collections.Generic; public class RandomPosition : MonoBehaviour { //Both must be same length public GameObject[] cubes; public List positions = new List(); void Start() { foreach (GameObject cube in cubes) { int randomIndex = Random.Range(0, positions.Count); cube.transform.position = positions[randomIndex]; positions.RemoveAt(randomIndex); } } } Instead of an array, use a list, so that every time you pick a position, you can remove it from the list (or make a copy first, if you want to maintain your array). This way you ensure, that a position can only be used once and the random range will always pick a number from within the items that are left.

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images