- Home /
How do I make the scene background have a "default" color?
I've been trying the whole day to fix this, but decided I would post here as a last resort. I have this set up:
void Start()
{ //cam = Camera.main
cam.backgroundColor = new Color32(56, 64, 72, 0);
if(colorchange == true)
{
currentColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
cam.backgroundColor = currentColor;
}
else
{
cam.backgroundColor = currentColor;
}
}
public void LoadNextLevel()
{
colorchange = true;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
void Restart()
{
colorchange = false;
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
Basically what it does is that when the player wins, the scene reloads the exact same but with a different color. And when the player dies, the color stays the same until they win again.
That's perfect, but there's one problem. When the scene initializes for the first time, 'colorchange' is false, so the scene starts totally black. How do I make it so that the first time the scene is loaded, the color is always a specific color? For example, always when I click play the color is gray, and change when the player dies/wins accordingly.
Sorry if I have over described or the question is too specific, but I am at a total loss as of now.
Thank you for reading and for any help provided!
Answer by Casiell · Feb 17, 2019 at 09:51 PM
Initialize currentColor with whatever color you want.
Is currentColor a static field?
@Casiell Alright I sort of fixed it by doing this:
void Start()
{
if (colorchange == true)
{
currentColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
cam.backgroundColor = currentColor;
}
else
{
cam.backgroundColor = currentColor;
}
}
static bool initialized = true;
void Update()
{
if(initialized == true)
{
initialized = false;
cam.backgroundColor = new Color32(56, 64, 72, 0);
}
}
It starts as the right color, but when the player dies in that scene, it goes back to black, which I think the only way of fixing it would be to make a different EndGame() method for the triggers that kill the player on that scene. Sorry if it was hard to understand, and advice is always appreciated.
Answer by zereda-games · Feb 17, 2019 at 11:54 PM
have you tried to do something like this :
private static Color32 currentColor= new Color32(56, 64, 72, 0);
public void ChangeColor(Color32 color){
currentColor=color;
}
maybe this
private static Color32 currentColor= new Color32(56, 64, 72, 0);
public cosnt Color32 DefaultColor= new Color32(56, 64, 72, 0);
void SetDefaultColor(){
currentColor=DefaultColor;
}
public void LoadNextLevel()
{
SetDefaultColor();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
and if you do this:
private static Color32 currentColor= new Color32(56, 64, 72, 0);
public cosnt Color32 DefaultColor= new Color32(56, 64, 72, 0);
//Use with button A
public void ChangeColor(Color32 color){
currentColor=color;
}
//Use with button A
public void LoadNextLevel()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
//Use with button B
public void SetDefaultColor(){
currentColor=DefaultColor;
}
Thank you for answering! Though that second one wouldn't work because every time the player won (or, loadednextlevel) the color would reset to 56, 64, 72, ins$$anonymous$$d of randomizing it. I'm working on a fix and will finish tomorrow, if it works I will make sure to share. By the way, what is that third script doing? I can't find out lol
ins$$anonymous$$d of 1 method doing both you would need to add both methods to the same button OnCall method.. that's all that's different there. also its just an example you would change the values to the desired values. i was quickly writing and just copy and pasting what was usable.
any of this should work with $$anonymous$$or adjustments. I don;t wanna write it all for you but the basic functionality is there
@pigaroos To randomize though, you could do a number of things... easiest i can see atm is make a list of colors and have a selection that gets set 2 random on scene change.
static List<Color>colors=new List<Color>(){Color.white,Color.black,Color.red,Color.blue,Colo.cyan,Color.yellow,Color.green,Color.grey,Color.magenta}
int selection=0;
Color currentColor=new Color();
void Awake(){
selection=Range(0,colors.Count)
currentColor=colors[selection];
}