Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by schadtaylor92 · May 28, 2014 at 01:35 AM · randomcolornewbieefficient

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 () {
     
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image schadtaylor92 · Jun 02, 2014 at 09:29 AM 0
Share

Thank you for a great working example, Clet.

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image schadtaylor92 · Jun 02, 2014 at 09:30 AM 0
Share

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.

avatar image Owen-Reynolds · Jun 02, 2014 at 12:55 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Material doesn't have a color property '_Color' 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges