- Home /
How can I make this more efficient?
This seems to achieve the desired effect - changing the color of 9 objects to red, yellow, and blue, while having 3 of each color. However, I believe it's inefficient. I am new to C# and Unity, as well as developing in general, so please don't get too wild.
using UnityEngine;
using System.Collections;
public class GenCubeColor : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject[] Cubes = GameObject.FindGameObjectsWithTag ("Cube");
GameObject RandomCube = Cubes [Random.Range (0, Cubes.Length)];
Color CubeColor = RandomCube.renderer.material.GetColor("_Color");
int Red = 0;
int Yellow = 0;
int Blue = 0;
for(int i = 0; i < Cubes.Length; i++) {
Cubes[i].renderer.material.color = Color.white;
}
while(Red < 3) {
if(CubeColor == Color.white) {
RandomCube.renderer.material.color = Color.red;
RandomCube = Cubes[Random.Range(0, Cubes.Length)];
CubeColor = RandomCube.renderer.material.GetColor("_Color");
Red = Red + 1;
}
else {
RandomCube = Cubes[Random.Range(0, Cubes.Length)];
CubeColor = RandomCube.renderer.material.GetColor("_Color");
}
}
while(Yellow < 3) {
if(CubeColor == Color.white) {
RandomCube.renderer.material.color = Color.yellow;
RandomCube = Cubes[Random.Range(0, Cubes.Length)];
CubeColor = RandomCube.renderer.material.GetColor("_Color");
Yellow = Yellow + 1;
}
else {
RandomCube = Cubes[Random.Range(0, Cubes.Length)];
CubeColor = RandomCube.renderer.material.GetColor("_Color");
}
}
while(Blue < 3) {
if(CubeColor == Color.white) {
RandomCube.renderer.material.color = Color.blue;
RandomCube = Cubes[Random.Range(0, Cubes.Length)];
CubeColor = RandomCube.renderer.material.GetColor("_Color");
Blue = Blue + 1;
}
else {
RandomCube = Cubes[Random.Range(0, Cubes.Length)];
CubeColor = RandomCube.renderer.material.GetColor("_Color");
}
}
Debug.Log("Red = " + Red);
Debug.Log("Yellow = " + Yellow);
Debug.Log("Blue = " + Blue);
}
// Update is called once per frame
void Update () {
}
}
Answer by Clet_ · May 28, 2014 at 02:09 AM
You're right. It's not very efficient. I'd be considering this approach instead
public class GenCube : MonoBehaviour {
private void Start () {
GameObject[] cubes = GameObject.FindGameObjectsWithTag ("Cube");
List<int> indices = new List<int> (cubes.Length);
for(int i = 0; i < cubes.Length; i++) {
indices.Add (i);
}
int count = 0;
while(indices.Count > 0) {
int randIndex = Random.Range (0, indices.Count);
if(count % 3 == 0) {
cubes[indices[randIndex]].renderer.material.color = Color.red;
} else if(count % 3 == 1) {
cubes[indices[randIndex]].renderer.material.color = Color.yellow;
} else {
cubes[indices[randIndex]].renderer.material.color = Color.blue;
}
count++;
indices.RemoveAt (randIndex);
}
}
}
That way, you won't constantly try to get a random cube until you get one that is white. Everything in this example is hardcoded, which is bad. With this approach, you basically throw away any reusability, but it does what you need for now.
Answer by Owen-Reynolds · May 28, 2014 at 02:34 AM
Why bother? You probably only do it once, or once every few seconds. And the "pick a number" loop isn't going to have too many extra rerolls.
If it makes sense to you, why make it harder to read? I'd preshuffle the indexes, then color them in order, but, again, always best to use code you completely understand.
But one obvious change, the last "blue loop" is really just turning the last three blue, so doesn't need to roll dice -- can just loop through all 9 and turn white ones blue. Or just start with them all blue -- turn 3 red, turn 3 yellow, and done.
The point in any of it is just to learn. I can't learn if I'm always sticking to what I know. Thank you for your advice about the ending loop.
Game programmers tend to be barely adequate. You can get "how do I do X" questions here pretty well, esp. if they are Unity specific. But you'll probably get better examples of algorithms on a general coding site (yes, your Q was just general program$$anonymous$$g.)
Your answer
Follow this Question
Related Questions
C# Randomize Background.Color Issues 1 Answer
Random colors for particles? 0 Answers
random Color. Glitch?!? 1 Answer
OnMouseEnter random hover color 4 Answers