Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I Change Camera Background in a Loop Multiple Times?

Discussion in 'Scripting' started by BsseeJ, Jun 16, 2014.

  1. BsseeJ

    BsseeJ

    Joined:
    Mar 12, 2014
    Posts:
    20
    Hello Unity Forum

    I am attempting to setup the background color of my camera so that it changes to several different colors in a loop. I can set it up to lerp between just two colors with math.pingpong but I can't figure out how to make it change in to several different colors to create a more interesting background that fits the theme of the game a bit better. I'm experimenting with this right now but it leaves it as white when I begin the application.

    Code (CSharp):
    1. void Awake () {
    2.         camera.backgroundColor = Color.white;
    3.         }
    4.  
    5. void Update() {
    6.         if (camera.backgroundColor == Color.white) {
    7.             camera.backgroundColor = Color.Lerp (Color.white, Color.green, Time.time *.1f);
    8.         }
    9.         if (camera.backgroundColor == Color.green) {
    10.             camera.backgroundColor = Color.Lerp (Color.green, Color.white, Time.time *.1f);
    11.                 }
    12.     }
    13. }
    14.  
     
  2. zee_ola05

    zee_ola05

    Joined:
    Feb 2, 2014
    Posts:
    166
    There is something wrong with your code.

    At the first Update step, your code will enter the first if statement, changing the color of your background to something near white. On its next update step, the code will enter neither of your if statements, thus your background is stuck to its near white color.

    Try using Coroutine to sequence the transition of the colors. Something like
    Code (CSharp):
    1. IEnumerator ColorTransition()
    2. {
    3.     while(camera.backgroundColor != Color.green)
    4.     {
    5.         camera.backgroundColor=Color.Lerp(Color.white, Color.green, Time.time*.1f);
    6.         yield return null;
    7.     }
    8.  
    9.     while(camera.backgroundColor != Color.white)
    10.     {
    11.         camera.backgroundColor=Color.Lerp(Color.green, Color.white, Time.time*.1f);
    12.         yield return null;
    13.     }
    14. }
     
  3. BsseeJ

    BsseeJ

    Joined:
    Mar 12, 2014
    Posts:
    20
    Okay I tried that and I messed around with it a bit. What happens is that it changes to green slowly, then instantly changes to white. I'm very new to programming and although I've read the Unity guide to IEnumerators I still have a hard time really understanding them. I could use more help if possible, maybe another solution? My end goal here is to have it switch between about 8 different colors via lerp, and if possible in a random order.
     
  4. Lars-Kristian

    Lars-Kristian

    Joined:
    Jun 26, 2013
    Posts:
    64
    Hi, I wanted something similar so I created a script for it.


    1. Populate the color list with two or more colors.
    2. Select a transition time (can not be 0).
    3. Select random colors or not.

    Background Color Script - demo.png

    Here is the script. You can always alter it, if it's not exactly what you are looking for.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class BackgroundColorScript : MonoBehaviour {
    6.  
    7.     public List<Color> colorList = new List<Color>();
    8.     public float transitionTime = 1f;
    9.     public bool randomColor = false;
    10.  
    11.     private int colorIndex = 0;
    12.     private int nextColorIndex = 0;
    13.  
    14.     //Init
    15.     void Awake() {
    16.         camera.backgroundColor = colorList[colorIndex];
    17.  
    18.         //Sets next color index
    19.         if(randomColor)
    20.         {
    21.             while(nextColorIndex == colorIndex)
    22.             {
    23.                 nextColorIndex = Random.Range(0, colorList.Count);
    24.             }
    25.         }
    26.         else
    27.         {
    28.             nextColorIndex += 1;
    29.             if(nextColorIndex >= colorList.Count)
    30.             {
    31.                 nextColorIndex = 0;
    32.             }
    33.         }
    34.  
    35.         //start transition if there are more than 1 colors in list
    36.         if(colorList.Count > 1)
    37.         {
    38.             StartCoroutine(ColorTransition());
    39.         }
    40.         else
    41.         {
    42.             Debug.LogWarning("Not enough colors in list.", this);
    43.         }
    44.     }
    45.  
    46.     IEnumerator ColorTransition()
    47.     {
    48.         //Init color transition
    49.         float delta = 0f;
    50.  
    51.         //color transition
    52.         while(delta < 1f)
    53.         {
    54.             delta += Time.deltaTime / transitionTime;
    55.             camera.backgroundColor=Color.Lerp(colorList[colorIndex], colorList[nextColorIndex], delta);
    56.             yield return new WaitForEndOfFrame();
    57.         }
    58.         camera.backgroundColor = colorList[nextColorIndex];
    59.  
    60.         //Sets next color index
    61.         colorIndex = nextColorIndex;
    62.         if(randomColor)
    63.         {
    64.             while(nextColorIndex == colorIndex)
    65.             {
    66.                 nextColorIndex = Random.Range(0, colorList.Count);
    67.             }
    68.         }
    69.         else
    70.         {
    71.             nextColorIndex += 1;
    72.             if(nextColorIndex >= colorList.Count)
    73.             {
    74.                 nextColorIndex = 0;
    75.             }
    76.         }
    77.  
    78.         //starts a new color transition
    79.         StartCoroutine(ColorTransition());
    80.     }
    81. }
     
  5. BsseeJ

    BsseeJ

    Joined:
    Mar 12, 2014
    Posts:
    20
    Wow that's a really well made script worked perfectly and easily. Thanks! I'm going to have to study it a bit to figure out how it works but it does what I need it to perfectly.
     
    Lars-Kristian likes this.
  6. LokiBoyi

    LokiBoyi

    Joined:
    Feb 28, 2017
    Posts:
    1
    When I used this code for my 2d sprite renderer component the sprite is vanishing. However i can see the color transition in the inspector
     
  7. DarthLeo

    DarthLeo

    Joined:
    Aug 16, 2021
    Posts:
    2
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ChangeColours : MonoBehaviour {
    4.     public Color color1 = Color.green;
    5.     public Color color2 = Color.blue;
    6.     public float duration = 3.5F;
    7.  
    8.     public Camera cam;
    9.  
    10.     private void  Update() {
    11.         var t  = Mathf.PingPong(Time.time, duration) / duration;
    12.         cam.backgroundColor = Color.Lerp(color1, color2, t);
    13.     }
    14. }