- Home /
C# Randomize Background.Color Issues
I've been trying to create a script which upon start randomizes the background color and changes as you move the scripted gameobject around. For some reason background.color resets to 255 instead of randomizing. Maybe start is causing this issue? Does random.range take longer than a single frame to execute properly?
using UnityEngine;
using System.Collections;
public class ColorChangeMove : MonoBehaviour {
// Use this for initialization
void Start () {
GetComponent<Camera>().backgroundColor = new Color(Random.Range(0, 255),Random.Range(0, 255),Random.Range(0, 255),5);
}
// Update is called once per frame
void LateUpdate () {
GetComponent<Camera>().backgroundColor = new Color(
GetComponent<Camera>().backgroundColor.r + transform.position.x * Time.smoothDeltaTime,
GetComponent<Camera>().backgroundColor.g + transform.position.y * Time.smoothDeltaTime,
GetComponent<Camera>().backgroundColor.b + transform.position.z * Time.smoothDeltaTime,
5
);
}
}
Answer by etopsirhc · Jul 18, 2015 at 09:17 PM
the transform pos times the delta Time might be high enough that when adding it to the previous background color it becomes white.
instead i would take the difference in the transform position between frames and add that to the random color. but even then it has a good chance to come up as white after a little while.
Your answer
Follow this Question
Related Questions
Change the background color attribute of a camera in C#? 2 Answers
HSV to RGB 1 Answer
Switch between random colors 4 Answers
Randomly Transition Camera Background Color? 2 Answers
How to randomly chenge camerabackground color every 5 seconds? 1 Answer