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 rawr13 · Oct 07, 2013 at 10:57 PM · colorbackgroundtransition

Randomly changing Background color

Hello I need help, im developing a 2D Platformer game ATM, and i tried many scripts and my own scripts to make a smooth, color transitioning background.. But no script worked, neither the few scripts that are in this forum. All are giving me errors like Internal compiler error or say that "there has to be a semicolon at the end", i did that but then the whole script went crazy.. I dont have an example now because i erased all of them in my rage :v

well.. heres one example from this forum, that doesnt work for me, but should do what i want:

 var bgColor : Color;    
 var blooping : boolean = true;
  
 function Start () {
     StartCoroutine(bgColorShifter());
 }
  
 function bgColorShifter()
 {
     while (blooping)
     {
         bgColor.r = Random.value; // value is already between 0 and 1
         bgColor.g = Random.value;
         bgColor.b = Random.value;
         bgColor.a = 1.0; // I don't think alpha matters    
         Debug.Log("bgColor: "+bgColor);
  
         var t: float = 0f
         var currentColor = Camera.main.backgroundColor;
         while( t < 1.0 )
         {
             Camera.main.backgroundColor = Color.Lerp(currentColor, bgColor, t );
             yield null; // Wait one frame
             t += Time.deltaTime;
         }
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by robertbu · Oct 08, 2013 at 05:20 AM

You are right in that fixing the ';' results in an internal compiler error. The issue is line 23: 'yield null;' It should be just 'yield'. But that kind of mistake should not causes an internal compiler error. Here is the code. I made a few largely cosmetic changes:

 #pragma strict
  
 function Start () {
    bgColorShifter();
 }
  
 function bgColorShifter()
 {
     var bgColor : Color; 
     
     while (true)
     { 
         bgColor = Color(Random.value, Random.value, Random.value, 1.0);
         
         var t: float = 0f;
         var currentColor = Camera.main.backgroundColor;
        
         while( t < 1.0 )
         {
             Camera.main.backgroundColor = Color.Lerp(currentColor, bgColor, t );
             yield;
             t += Time.deltaTime;
         }
     }
 }
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 drudiverse · Jun 20, 2014 at 08:00 AM

heres a script to change the background via HSV controls

hsv is nice version of random color, based on natural colors.

 #pragma strict
 var Hcol :float;
 var Scol :float;
 var Vcol :float;
 
 //var SPercent :float;
 //var VPercent :float;
 
 
 function Start () {
 
 // Hcol = Random.value;
 //Scol = Random.value    ;
 //Vcol = Random.value + VPercent  / 100   ;
 
 Camera.main.backgroundColor = HSVtoRGB(Hcol,Scol,Vcol);
 }
 
 function Update () {
 Camera.main.backgroundColor = HSVtoRGB(Hcol,Scol,Vcol);
 print (HSVtoRGB(Hcol,Scol,Vcol));
 print (HSVtoRGB(Hcol,Scol,Vcol));
 }
 
 function HSVtoRGB(h : float, s : float, v : float) {
 
 
     h=h%1;
     s=s%1;
     v=v%1;
     var r : float;
     var g : float;
     var b : float;
     var i : float;
     var f : float;
     var p : float;
     var q : float;
     var t : float; 
     i = Mathf.Floor(h * 6);
     f = h * 6 - i;
     p = v * (1 - s);
     q = v * (1 - f * s);
     t = v * (1 - (1 - f) * s);
     switch (i % 6) {
         case 0: r = v; g = t; b = p; break;
         case 1: r = q; g = v; b = p; break;
         case 2: r = p; g = v; b = t; break;
         case 3: r = p; g = q; b = v; break;
         case 4: r = t; g = p; b = v; break;
         case 5: r = v; g = p; b = q; break;
     }
     return Color(r,g,b); 
 }
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.

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

but its not changing smoothly. How to do that?

avatar image YoungDeveloper · Aug 21, 2015 at 11:51 AM 0
Share

@aksh2143 use time delta and it property best in coroutine or update

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

18 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

Related Questions

{Solved}Trying to add color loop to background using Vector4 2 Answers

Change the background color attribute of a camera in C#? 2 Answers

Specify background color of Gui.Button at runtime 1 Answer

C# Randomize Background.Color Issues 1 Answer

2D - draw sprite only on one background color 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