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 kidock · Jan 14, 2014 at 10:05 PM · randomcolorrandom.range

randomly change between 6 colors C#

I want to change a material color between 6 different colors. I tried many things:

 using UnityEngine;
 using System.Collections;
 
 public class changecolor : MonoBehaviour 
 {
 
     void Star()
     {
 
         Color.cyan = 1;
         Color.red = 2;
         Color.green = 3;
         new Color(255, 165, 0) = 4;
         Color.yellow = 5;
         Color.magenta = 6;
     }
     void OnTriggerEnter(Collider other)
     {
 
         gameObject.renderer.material.color = Random.Range(1, 6);
     }
 }
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

4 Replies

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

Answer by robertbu · Jan 15, 2014 at 01:30 AM

You want to use an array. Try this:

 using UnityEngine;
 using System.Collections;
 
 public class changecolor : MonoBehaviour 
 {
     Color[] colors = new Color[6];
     
     void Start()
     {
         colors[0] = Color.cyan;
         colors[1] = Color.red;
         colors[2] = Color.green;
         colors[3] = new Color(255, 165, 0);
         colors[4] = Color.yellow;
         colors[5] = Color.magenta;
     }
     void OnTriggerEnter(Collider other)
     {
         gameObject.renderer.material.color = colors[Random.Range(0, colors.Length)];
     }
 }

Note if you made colors 'public' you could get rid of the Start() and just pick the colors in the Inspector. Once this scripts is attached to the game object, set the size of the 'colors' array in the Inspector and then set each individual color.

 using UnityEngine;
 using System.Collections;
 
 public class changecolor : MonoBehaviour 
 {
     public Color[] colors;
     
     void OnTriggerEnter(Collider other)
     {
         gameObject.renderer.material.color = colors[Random.Range(0, colors.Length)];
     }
 }
Comment
Add comment · Show 3 · 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 kidock · Jan 21, 2014 at 01:03 AM 0
Share

after some time I got the same code by my self but still had an error, it was because I was saying gameObject.renderer.material.color = color[Random.Range(0, colors.Length)]; ins$$anonymous$$d of gameObject.renderer.material.color = colors[Random.Range(0, colors.Length)];

avatar image ShiftRapidz · Dec 01, 2018 at 05:46 PM 0
Share

Error CS1061 'Component' does not contain a definition for 'material' and no accessible extension method 'material' accepting a first argument of type 'Component' could be found (are you missing a using directive or an assembly reference?)

Error CS0619 'GameObject.renderer' is obsolete: 'Property renderer has been deprecated. Use GetComponent() ins$$anonymous$$d. (UnityUpgradable)'

i get these two errors any help??

avatar image Hellium ShiftRapidz · Dec 01, 2018 at 06:48 PM 0
Share
  void OnTriggerEnter(Collider other)
  {
      GetComponent<Renderer>().material.color = colors[Random.Range(0, colors.Length)];
  }
avatar image
0

Answer by knobblez · Dec 01, 2018 at 09:25 PM

Could use a switch.

 public int chosencolor;
 
 void choosecolor()
 {
 chosencolor = random.range(0,5);
 
 switch(chosencolor)
 {
 case 0:
 //red
 break;
 case 1:
 //green
 break;
 case 2:
 //blue
 break;
 {
 }
 
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
avatar image
0

Answer by Vrushank26 · Dec 08, 2015 at 03:56 AM

How do i select color linearly from array ? means i apply this on multiple object so all object's color will be changed together

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

Answer by sandeepsmartest · Dec 08, 2015 at 04:59 AM

 public GameObject [] ObjsDatChangeColor;//assign all Gameobjects to this
 public Color [] ColorsArr;//assign all colors to this array
 public int RequiredColorIndex;//index of the array in which desired color located
 void OnTriggerEnter(Collider other)
     {
         for (int i = 0; i < ObjsDatChangeColor.Length; i++) {
             ObjsDatChangeColor[i].GetComponent<Renderer>().material.color = ColorsArr[RequiredColorIndex];
         }
         
     }


Hope this may help you. NSKS

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 Vrushank26 · Dec 08, 2015 at 12:54 PM 0
Share

Look, there are two cube prefab(both Prefab has same material) which are called by script in game, Now i have to change their color with respect to t time. and i have to add 5-6 color in this. For Ex:- every 10 s both cube prefab change their color from white to red after that 10s both change their color from red to yellow and so on. So now tell how to make script for it?

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

24 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

Related Questions

Random objects don't overlap 1 Answer

Random.Range is not changing? 1 Answer

C# Randomize Background.Color Issues 1 Answer

Randomising list of integers and removing them from list once the task assigned to the integer in the project is completed 0 Answers

Random colors for particles? 0 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