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 usename · Dec 14, 2013 at 04:39 PM · randomcolorbackgroundmaterial color

Switch between random colors

I would like to make my background change between different random colors. The script I have right now is this. This script only changes between two colors which are not random.

 #pragma strict
 
 var RandomMusic : int;
 var colorStart : Color;
     var colorEnd : Color;
     var duration : float = 1.0;
     function Update () {
         var lerp : float = Mathf.PingPong (Time.time, duration) / duration;
         renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
     }
     if (RandomMusic < 8); {
         colorStart = Color.cyan; 
         colorEnd = Color.black; 
     } 
     if (RandomMusic  < 8); {
         colorStart = Color.red;
         colorEnd = Color.green; 
     }
 
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
3
Best Answer

Answer by tanoshimi · Dec 22, 2013 at 08:34 PM

The reason why the previous answers are only ever switching between the same two random colours is because they are only ever assigning colorStart and colorEnd once, in the Start() function. Try this instead:

 #pragma strict
 var colourStart: Color;
 var colourEnd: Color;
 var rate: float = 1; // Number of times per second new colour is chosen
 var i : float = 0; // Counter to control lerp
      
 function Start() {
      colourStart = new Color(Random.value, Random.value, Random.value);
     colourEnd = new Color(Random.value, Random.value, Random.value);
 }
      
 function Update () {
 
     // Blend towards the current target colour
       i += Time.deltaTime*rate;
     renderer.material.color = Color.Lerp (colourStart, colourEnd, i);
     
     // If we've got to the current target colour, choose a new one
     if(i >= 1) {
         i = 0;
         colourStart = renderer.material.color;
         colourEnd = new Color(Random.value, Random.value, Random.value);
     }
 }
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 usename · Dec 22, 2013 at 09:34 PM 0
Share

Thank you! it works perfectly

avatar image tanoshimi · Dec 22, 2013 at 10:48 PM 0
Share

You're welcome :)

avatar image Sajalsh25 · Jul 21, 2016 at 10:11 AM 0
Share

Thanks A Ton :) Worked like a charm!

avatar image
0

Answer by KellyThomas · Dec 14, 2013 at 04:48 PM

Random.value can help you do this:

 var randomColor: Color = new Color(Random.value, Random.value, Random.value);


Comment
Add comment · Show 4 · 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 usename · Dec 22, 2013 at 08:16 AM 0
Share

So why doesn't this work? sorry for the late response. It just changes back and forward from the same colors. I tried the script from the first answer to my question and that works but it changes between the colors in a weird way. The way it changes this script is the way I want it to change. It needs to have that fade effect ins$$anonymous$$d of just fast chaning to another color. How can I do this?

 #pragma strict
  
 var Random$$anonymous$$usic : int;
 var colorStart : Color = new Color(Random.value, Random.value, Random.value);
     var colorEnd : Color = new Color(Random.value, Random.value, Random.value);
     var duration : float = 1.0;
     function Update () {
        var lerp : float = $$anonymous$$athf.PingPong (Time.time, duration) / duration;
        renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
     }
avatar image KellyThomas · Dec 22, 2013 at 08:30 AM 0
Share

Try this:

 #pragma strict
  
 var Random$$anonymous$$usic: int;
 var colorStart: Color;
 var colorEnd: Color;
 var duration: float = 1.0;
 
 function Start() {
     colorStart =  new Color(Random.value, Random.value, Random.value);
     colorEnd = new Color(Random.value, Random.value, Random.value);
 }
 
 function Update () {
     var lerp: float = $$anonymous$$athf.PingPong (Time.time, duration) / duration;
       renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp);
 }


I suspect the way unity uses the inspector to initialise public variables might be messing things up for you.

avatar image usename · Dec 22, 2013 at 08:02 PM 0
Share

This should work but for some reason it doesn't. It just keeps chaning between the two different colors. It does the fade effect which is good. In the inspector it contains 4 options. The Random$$anonymous$$usic which is set to 0 and the Color Start which is set to blue and the Color End which is set to green and the Duration which is set to one. Does the color variables need to be some specific value in order to work?

avatar image KellyThomas · Dec 22, 2013 at 10:12 PM 0
Share

Oh, you want an endless series or random colours?

 var colorStart: Color;
 var colorEnd: Color;
 var colorTransitionTime: float = 10.0;
 var colorElapsedTime: float = 0.0;
 
 
 function randomColor (): Color {
      return new Color(Random.value, Random.value, Random.value); 
 }

 
 function Start() {
     colorStart = randomColor();
     colorEnd = randomColor();
 }
 
 function Update () {
     colorElapsedTime += Time.deltaTime;
     while (colorElapsedTime > colorTransitionTime) {
         colorStart = colorEnd;
         colorEnd = randomColor();
         colorElapsedTime -= colorTransitionTime;
     }
     renderer.material.color = Color.Lerp (colorStart, colorEnd, colorElapsedTime / colorTransitionTime);
 }
avatar image
0

Answer by robertbu · Dec 14, 2013 at 04:46 PM

You can get a random color between two specified colors by:

 var randColor =  Color.Lerp (colorStart, colorEnd, Random.value);

Your code above has a number of issues, and I don't think you want to change the color every frame. Concentrating on only the color change you can do:

 #pragma strict
  
 var colorStart : Color;
 var colorEnd : Color;
 var duration : float = 1.0;
 
 function ChangeColor() {
     renderer.material.color =  Color.Lerp (colorStart, colorEnd, Random.value);
 }
 
 function Start() {
     InvokeRepeating("ChangeColor", 0.0, duration);
 }

Make sure you have the alpha for colorStart and colorEnd set to 255.

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 aksh2143 · Aug 21, 2015 at 11:39 AM

i got it.

using System.Collections;

public class Camera_cript : MonoBehaviour { Color bgcolor; Color current; public Camera camera1;

public void Start () { InvokeRepeating ("Change_color", 0.0f, 5.0f); }

 void Change_color()
 {
     current = new Color (Random.value, Random.value, Random.value);
     bgcolor = new Color (Random.value, Random.value, Random.value);
     camera1.backgroundColor = Color.Lerp (current, bgcolor, 5.0f);

 }

}

Thats it and it'll change every 5 seconds

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

21 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

Related Questions

C# Randomize Background.Color Issues 1 Answer

weird color or lighting bug in Unity? 1 Answer

How to change color of sprite as it moves over different backgrounds 2 Answers

Change Scene Color : How to acess color correction curves from a c# script 0 Answers

Why colors are being mixed after I try to change them? 1 Answer


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