- Home /
How do I make the elements of my array random?
Ive got an array with 3 colours and I want them to be randomly assigned to game objects spawning in but not sure how to do this. Any help would be amazing!
Thanks
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Main : MonoBehaviour {
public static float Range;
public GameObject[] squaresDownArray;
public GameObject[] squaresUpArray;
public GameObject square;
int index;
public Color[] colourArray;
public Vector2 leftSpawnPosition;
public Vector2 midSpawnPosition;
public Vector2 rightSpawnPosition;
public float spawnTimer;
private float spawnCounter;
private int squareIndexCounter;
// Use this for initialization
void Start () {
//squaresDownArray = GameObject.FindGameObjectsWithTag ("SquareDown");
//squaresUpArray = GameObject.FindGameObjectsWithTag ("SquareUp");
InvokeRepeating ("TimerTick", 0, 1);
}
// Update is called once per frame
void Update () {
spawnCounter += Time.deltaTime;
}
public void TimerTick(){
if(spawnCounter >= spawnTimer){
GameObject lSquare = Instantiate (square, leftSpawnPosition, Quaternion.identity);
lSquare.tag = "SquareDown";
//lSquare.GetComponent<SpriteRenderer> ().color = colourArray [squareIndexCounter];
lSquare.GetComponent<MeshRenderer>().materials[0].color = colourArray [squareIndexCounter];
squareIndexCounter++;
if (squareIndexCounter >= colourArray.Length) {
squareIndexCounter = 0;
}
GameObject mSquare = Instantiate (square, midSpawnPosition, Quaternion.identity);
mSquare.tag = "SquareUp";
//mSquare.GetComponent<SpriteRenderer> ().color = colourArray [squareIndexCounter];
mSquare.GetComponent<MeshRenderer>().materials[0].color = colourArray [squareIndexCounter];
squareIndexCounter++;
if (squareIndexCounter >= colourArray.Length) {
squareIndexCounter = 0;
}
GameObject rSquare = Instantiate (square, rightSpawnPosition, Quaternion.identity);
rSquare.tag = "SquareDown";
//rSquare.GetComponent<SpriteRenderer> ().color = colourArray [squareIndexCounter];
rSquare.GetComponent<MeshRenderer>().materials[0].color = colourArray [squareIndexCounter];
squareIndexCounter++;
if (squareIndexCounter >= colourArray.Length) {
squareIndexCounter = 0;
}
spawnCounter -= spawnTimer;
}
foreach (GameObject square in GameObject.FindGameObjectsWithTag("SquareUp")) {
square.GetComponent<SquareScript> ().Move ("up");
}
foreach (GameObject square in GameObject.FindGameObjectsWithTag("SquareDown")) {
square.GetComponent<SquareScript> ().Move ("down");
}
}
}
Answer by toddisarockstar · Mar 24, 2017 at 04:27 AM
here is the color i believe you are looking for :
colourArray[UnityEngine.Random.Range(0,colourArray.Length)];
Hi, where abouts in the code would this go? Sorry, im new to Unity!
Thanks!
I believe that's what you are looking for.
rSquare.GetComponent().materials[0].color = colourArray[UnityEngine.Random.Range(0,colourArray.Length)];
Answer by Pan_Migo · Mar 28, 2017 at 07:09 AM
Well as mentioned above you just need to use Random.Range().
However to it's always better to have a clean code so If I were you I would have created a method to do that job. Remember every method must execute ONLY what it's name implies.
public Color AssignRandomColor(){
return ColorArray[Random.Range(0,colourArray.length)] ;
}
Then whenever you want to get a random color you can just call this method and a random color will be returned.
e.g
mSquare.GetComponent<MeshRenderer>().materials[0].color = AssignRandomColor();
Hope this helps!!!!
Your answer
Follow this Question
Related Questions
question about arrays 1 Answer
Beginner : Random gameobject from array 2 Answers
C# Random spawn from an array 0 Answers
Referencing private string for array name 2 Answers
Writing to GUI from an array 0 Answers