Camera.backgroundColor doesnt actually work

Hello i wanna simply change background color by code, in future it will be very important. Problem is that everytime i use the cam.backgroundColor, it doesnt really change the color visibly, but in debug.log(“” + cam.backgroundColor) it actually show the right changed color and my background color in inspector only changes alfa that is actually not visible with background colors.-

public Camera cam;

    void Start()
    {
        cam.clearFlags = CameraClearFlags.SolidColor;
    }
    void Update()
    {
        if (Input.GetKey("f"))
        {
            cam.backgroundColor = new Color(0f, 1f, 0f, 1f);
            Debug.Log("Background");
        }
        Debug.Log("" + cam.backgroundColor);
    }

Your code works perfectly for me. Can you post a screenshot of the camera’s settings in the Inspector?

So as you can see, my camera is child object. It has the script below and in Console you can see that my color should be 0,1,0 but it obviously isnt.

Even on a child camera, it still works for me. But I can see there is more to your code than you posted. The BackgroundColorManager editor box shows at least one more public member for your code in the Inspector, and the console output shows your debug message is coming from Line 26 of your code, not Line 14 of what you posted here.

Post your complete code, so I can see what’s really going on.

This is the whole script but i dont find the rest usefull at any way ./ Could thep problem be that im using 2020.1.2f1 version ?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BackgroundColorManager : MonoBehaviour
{
    public Camera cam;

    public bool on = true;

    void Start()
    {
        cam.clearFlags = CameraClearFlags.SolidColor;
    }
    void Update()
    {
        if (Input.GetKey("f"))
        {
            on = false;
        }
        if (on == false)
        {
            cam.backgroundColor = new Color(0f, 1f, 0f, 1f);
            Debug.Log("Background");
        }
        Debug.Log("" + cam.backgroundColor);
    }
}

I doubt that the version matters. This is pretty basic stuff you’re doing, although your code is a little convoluted.

Try dropping Main Camera into the cam field and look at its background in the Inspector. Does your code work then?

The main camera is just empt parent object, it has no camera component.

Okay now it seems to work when i disabled my animator, it had old animation where i wa trying to change background color by animator. I would have realised this but the color changing animation werent triggred and they werent even switching to this color so idk what happend but its working now.

Glad you got it working. As you can see, it is hard to debug a problem when the code you are working with is part of a larger project. It is always best to strip out as much as you can while still being able to reproduce the problem. At some point, if the problem is being caused by other code (as it was here), when you strip out that other code and the problem goes away, you’ll know the problem is in the code you just stripped out, not the code you were originally working on.

And, you’re welcome.

1 Like

Thanks for help :slight_smile: