Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 UniluckStudios · Aug 10, 2017 at 02:55 AM · scripting problemrandomcolorcolor changerandomize

Make sure that game objects aren't the same color

Ok in my game I have 4 balls. I have a script that makes the balls change to a random color. How could I make sure that none of the balls are the same color? Here is my script: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Everything : MonoBehaviour { public string currentColor; public SpriteRenderer sr; public SpriteRenderer sr1; public SpriteRenderer sr2; public SpriteRenderer sr3; public SpriteRenderer sr4; public Color colorGreen; public Color colorRed; public Color colorBlack; public Color colorBlue; public Color colorOrange; public Color colorYellow; public Color colorBrown; // Use this for initialization

 void Start () {
     SetRandomColor ();

 }
 
 // Update is called once per frame
 void Update () {
     
 }

 void SetRandomColor () {
     sr1.color != sr2.color;
     sr1.color != sr3.color;
     sr1.color != sr4.color;
     sr2.color != sr3.color;
     sr2.color != sr4.color;
     sr3.color != sr4.color;
     int index = Random.Range (0, 6);
     switch (index) {
         case 0:
         currentColor = "Green";
         sr.color = colorGreen;
         break;
     
         case 1:
         currentColor = "Red";
         sr.color = colorRed;
         break;

         case 2:
         currentColor = "Black";
         sr.color = colorBlack;
         break;

         case 3:
         currentColor = "Blue";
         sr.color = colorBlue;
         break;

         case 4:
         currentColor = "Orange";
         sr.color = colorOrange;
         break;

         case 5:
         currentColor = "Yellow";
         sr.color = colorYellow;
         break;

         case 6:
         currentColor = "Brown";
         sr.color = colorBrown;
         break;
     
     }

     int help = Random.Range (0, 6);
     switch (help) {
     case 0:
         currentColor = "Green";
         sr1.color = colorGreen;
         break;

     case 1:
         currentColor = "Red";
         sr1.color = colorRed;
         break;

     case 2:
         currentColor = "Black";
         sr1.color = colorBlack;
         break;

     case 3:
         currentColor = "Blue";
         sr1.color = colorBlue;
         break;

     case 4:
         currentColor = "Orange";
         sr1.color = colorOrange;
         break;

     case 5:
         currentColor = "Yellow";
         sr1.color = colorYellow;
         break;

     case 6:
         currentColor = "Brown";
         sr1.color = colorBrown;
         break;
     }

     int yes = Random.Range (0, 6);
     switch (yes) {
     case 0:
         currentColor = "Green";
         sr2.color = colorGreen;
         break;

     case 1:
         currentColor = "Red";
         sr2.color = colorRed;
         break;

     case 2:
         currentColor = "Black";
         sr2.color = colorBlack;
         break;

     case 3:
         currentColor = "Blue";
         sr2.color = colorBlue;
         break;

     case 4:
         currentColor = "Orange";
         sr2.color = colorOrange;
         break;

     case 5:
         currentColor = "Yellow";
         sr2.color = colorYellow;
         break;

     case 6:
         currentColor = "Brown";
         sr2.color = colorBrown;
         break;
     }

     int omg = Random.Range (0, 6);
     switch (omg) {
     case 0:
         currentColor = "Green";
         sr3.color = colorGreen;
         break;

     case 1:
         currentColor = "Red";
         sr3.color = colorRed;
         break;

     case 2:
         currentColor = "Black";
         sr3.color = colorBlack;
         break;

     case 3:
         currentColor = "Blue";
         sr3.color = colorBlue;
         break;

     case 4:
         currentColor = "Orange";
         sr3.color = colorOrange;
         break;

     case 5:
         currentColor = "Yellow";
         sr3.color = colorYellow;
         break;

     case 6:
         currentColor = "Brown";
         sr3.color = colorBrown;
         break;
     }


     int yay = Random.Range (0, 6);
     switch (yay) {
     case 0:
         currentColor = "Green";
         sr4.color = colorGreen;
         break;

     case 1:
         currentColor = "Red";
         sr4.color = colorRed;
         break;

     case 2:
         currentColor = "Black";
         sr4.color = colorBlack;
         break;

     case 3:
         currentColor = "Blue";
         sr4.color = colorBlue;
         break;

     case 4:
         currentColor = "Orange";
         sr4.color = colorOrange;
         break;

     case 5:
         currentColor = "Yellow";
         sr4.color = colorYellow;
         break;

     case 6:
         currentColor = "Brown";
         sr4.color = colorBrown;
         break;
     }
 }

}

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

Answer by toddisarockstar · Aug 10, 2017 at 03:28 AM

try this. balls will never be the same color

     public SpriteRenderer sr; 
     public SpriteRenderer sr1; 
     public SpriteRenderer sr2; 
     public SpriteRenderer sr3; 
     public SpriteRenderer sr4; 
     public SpriteRenderer[] balls;
     int i;int i2;
     public Color[] colors = new Color[]{Color.red,Color.green,Color.yellow,Color.black,Color.cyan,Color.magenta,Color.white};
     public string used;
     void Start () {
         balls = new SpriteRenderer[]{sr,sr1,sr2,sr3,sr4};
         used = "";
         i = balls.Length;
         while(i>0){i--;
             bool b = false;
             while(!b){
                 i2=Random.Range(0,colors.Length);
                 if(!used.Contains(" "+i2))
                 {b=true;used=used+" "+i2;}}
             balls[i].color=colors[i2];}}
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 toddisarockstar · Aug 10, 2017 at 04:21 AM 0
Share

i edited my answer to fit what you are doing a bit better!

avatar image
0

Answer by KenjiKyo · Aug 17, 2017 at 11:38 PM

How about having a List<Color> that you initialize with all desired colors that can be chosen from. Some kind of color selection pool. Call Random.Range() between 0 and the list size to get a random color index, and remove that color from the list to apply it to any given ball. If a ball has to change to a new color, re-insert its color into the list to make it available for future random color picking.

Comment
Add comment · 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

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

114 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

How do I randomly create 2 objects, each with a different shape and color? 1 Answer

how to change color in unity scripting overtime?,how to change color gradually with C# 2 Answers

Randomize a spawn position without repition 3 Answers

Randomize texture within prefab 0 Answers

Color.lerp not working properly 2 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