Forum rules - please read before posting.

Culling Mask, Skybox, and Background colors on game cameras

I need to change the Culling Mask and Background Colors on certain game cameras. How would I go about doing that? Currently, the changes I make to the game cameras are overwritten by the main camera at runtime.

Comments

  • The MainCamera copies various properties of the GameCamera it's attached to, but for performance it doesn't do everything.  You can get around this with a simple script attached to the MainCamera that copies over the rest, eg:

    using AC;

    MainCamera mainCamera;

    void Awake ()
    {
      mainCamera = GetComponent <MainCamera>();
    }

    void Update ()
    {
      if (mainCamera.attachedCamera)
      {
        camera.backgroundColor = mainCamera.attachedCamera.backgroundColor;
        camera.cullingMask = mainCamera.attachedCamera.cullingMask;
      }
    }

  • Hey Chris

    I have a similar issue where I need to use the Culling Mask on my cameras. I tried adding the above script to the MainCamera but I'm getting the following error:

    Assets/NewBehaviourScript.cs(2,12): error CS0116: A namespace can only contain types and namespace declarations

    Any idea why? I'm not very good with scripting :P
  • Not sure if this is the same with the skybox but I could change the skybox easily with the following script. Object send message, custom, and put an integer (1).



  • edited April 2015
    I cant seem to paste the code, thats strange...
  • I'm still having trouble with enabling Culling Mask on the cameras. Has anyone managed to get this working?
  • edited September 2015
    @mikeblythe: Try this, in a C# script named ChangeCullingMask:

    using AC;

    public class ChangeCullingMask : MonoBehaviour
    {

      MainCamera mainCamera;

      void Awake ()
      {
        mainCamera = GetComponent <MainCamera>();
      }

      void Update ()
      {
        if (mainCamera.attachedCamera)
        {
          camera.backgroundColor = mainCamera.attachedCamera.backgroundColor;
          camera.cullingMask = mainCamera.attachedCamera.cullingMask;
        }
      }
    }
  • Hey there,

    This is a bit of an old topic, but I've found a need to copy the culling mask properties over from my game cameras and can't get this working. There are two things going on.

    First, I'm getting an error on  'camera.cullingMask' and 'camera.backgroundColor' that says it is obsolete. However, when I change them to 'GetComponent<Camera>().cullingMask' or 'GetComponent<Camera>().backgroundColor',  I get an error saying: '_Camera does not contain a definition for 'cullingMask' and no extension method 'cullingMask' accepting a first argument of type '_Camera' could be found. -- and the same error for 'backgroundColor' 

    I really REALLY don't want to mess around with the _Camera code. Am I missing an easier way of doing this? 

  • edited October 2016
    You can try this (it worked on my side. You can put it in any object in the hierarchy):

    using UnityEngine;
    using AC;

    public class AC_CameraIntegration : MonoBehaviour
    {
        //AC main Camera
        MainCamera _mainCamera;

        void Awake()
        {
            //get the AC Main Camera component from the current Unity MainCamera.
            _mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<MainCamera>();
        }

        void Update()
        {
            if (_mainCamera.attachedCamera != null)
            {
                Camera.main.backgroundColor = _mainCamera.attachedCamera.GetComponent<Camera>().backgroundColor;
                Camera.main.cullingMask = _mainCamera.attachedCamera.GetComponent<Camera>().cullingMask;

                //add more properties here if you want.
            }
        }
    }
  • edited October 2016
    @Alverik! You are the best! I'll give it a shot. 

    ETA: Worked like a charm :)
  • edited October 2016
    Glad you liked it and that I could be of help :) 
    I Hope it's useful for others too.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.