- Home /
Random cubes
so, i have 25 different-coloured cubes, all of the cubes have this c# script attached to them
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RandomPosition : MonoBehaviour {
public Vector3[] positions;
void Start () {
int randomNumber=Random.Range(0,positions.Length);
transform.position = positions[randomNumber];
}
}
this script, every time i start the game, change the position of my cubes in one of the 25 positions that i made, the problem is that a lot of cubes go to the same position, and i don't want them to be in the same place. How do i do? please help me, and sorry for my bad english. :')
Answer by Xarbrough · Apr 04, 2015 at 10:25 PM
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<Vector3> positions = new List<Vector3>();
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.
how do i make an array or a list? however after made this list i have to add this script to the list right? can you explain what i have to do a little bit better? I am a noob with unity :P. Anyway thanks for the help ;)
You create an empty GameObject and attach my RandomPosition script to it. Then you drag all of your 25 cubes into the inspector field "cubes". That is the most easy version to get started. Next you will probably want to instantiate your cubes from a single prefab, or if you're already doing so, you would just have to to loop through your array and add them while instantiating. This is how it could work:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RandomPosition : $$anonymous$$onoBehaviour {
public GameObject cubePrefab;
public int numberOfCubes;
public List<Vector3> positions = new List<Vector3>();
void Start() {
for (int i = 0; i < numberOfCubes; i++) {
int randomIndex = Random.Range(0, positions.Count);
Instantiate(cubePrefab, positions[randomIndex], Quaternion.identity);
positions.RemoveAt(randomIndex);
}
}
}
the console show to me this problem, when i apply to my gameobject the first script that you send to me:
Assets/RandomPosition1.cs(5,15): error CS0101: The namespace global::' already contains a definition for RandomPosition'
There cannot be multiple script files with the same name, every class must be unique. Remove either your old RandomPosition script or rename the new one to RandomPositionNew until you can come up with a good name. You will have to change the name of the script file in the project, as well as the class name in the file itself.
i have same problem and i try your script in my game but i dont understand why are all my cubes within together on 0,0,0 coordinates.
i really need help.
Answer by hbalint1 · Apr 04, 2015 at 09:39 PM
Hello. Try changing the random seed, so it gives real random numbers every time. Put one of these to the first line of Start method:
Random.seed = (int)System.DateTime.Now.Ticks;
or
Random.seed = System.Environment.TickCount;
Your answer
Follow this Question
Related Questions
How to make an object/prefab choose between multiple given positions to spawn into? 1 Answer
Adding Imperfections? 1 Answer
Mapping Cube to Sphere - Using 64 planes as a cube "side" 1 Answer
How can I show a random integer on a cube 1 Answer
How can i destroy cubes that is randomly generated when i have passed them? 1 Answer