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

[ExecuteAlways]
public class SkyBox : MonoBehaviour
{
    public Material[] skyboxes;
    public Camera skyboxCamera;
    public float skyboxMoveSpeed = 2f;

    private int index = 0;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            SwitchSkybox();
        }

        if (RenderSettings.skybox == skyboxes[1])
        {
            RenderSettings.skybox.SetFloat("_Rotation", Time.time * skyboxMoveSpeed);
        }
    }

    public void SwitchSkybox()
    {
        index++;
        if (index == skyboxes.Length)
        {
            index = 0;
        }
        RenderSettings.skybox = skyboxes[index];

        if (RenderSettings.skybox == skyboxes[1])
        {
            skyboxCamera.enabled = true;
            Camera.current.enabled = false;
            Time.timeScale = 1.0f;
        }
        else
        {
            skyboxCamera.enabled = false;
            Camera.current.enabled = true;
            Time.timeScale = 0.0f;
        }
    }
}

The script switch between skyboxes the default and my skybox and also switch between the currently active camera and the sky box camera.

But when I'm hitting the escape key it's throwing null exception in the editor on the line number 46 :

Camera.current.enabled = false;

The current of the Camera is null

I want to make that when I press the escape key it will switch to my skybox and to the skybox camera and also will pause the game (Later I will make a main menu when the game is paused).

Jack Mariani
  • 2,270
  • 1
  • 15
  • 30
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • 1
    Maybe [this](http://answers.unity.com/answers/173563/view.html) answer on the unity Q&A site will help – Ruzihm Jul 29 '19 at 21:38
  • `Camera?.current?.enabled = value;` will short-circuit without an error if `Camera` or `current` are `null`. – Rufus L Jul 29 '19 at 21:51
  • Is this while it is running or in the editor? – AresCaelum Jul 29 '19 at 21:52
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Stefan Jul 29 '19 at 21:54
  • 1
    If you reword the question to be about the life cycle of the camera object, I think it will attract more upvotes. – Stefan Jul 29 '19 at 22:03
  • 1
    I looked at the unity docs and updated my answer if you want to give it another look – vasmos Jul 29 '19 at 22:13
  • @Ruzihm Answer I think is the right one. I'll better assign and use my own camera instead Camera.current or main just to add another public Camera variable and assign the camera I want to switch. – Daniel Lip Jul 29 '19 at 22:14
  • @Stefan I didn't ask what is null but why I'm getting it. – Daniel Lip Jul 29 '19 at 22:21
  • 1
    @MuhamadAbutShtil: I see that, but your wording is highly controvesial. I am just giving you a word of advise on how your question can be more well received. Your case is about the camera's object lifetime. Not about the null ref. – Stefan Jul 29 '19 at 22:26

2 Answers2

5

This is the Camera.current, from the manual.

The camera we are currently rendering with.

Also worth noting the comment from Ruzihm.

The Unity engine typically assigns an already-instantiated instance of Camera to Camera.current

So, from your scripts, I see 2 issues. The one directly related to this questions happens just in editor mode and I will start from that one.

Editor Issue: Camera.current is null

When working in the editor, Camera.current won't be just your own application's camera, but it could be any camera. It could even refer to the editor's scene view camera.
In this last case, if your scene view is not in focus (IE when you've focus on Game Window) Camera.current will be null.

Logical Issue: you couldn't switch back

When you try to switch back from skyboxCamera, your Camera.current will be the same skyboxCamera, and not your default camera. So you won't be able to retrieve the previous camera.


SOLUTION

Do not use Camera.current, but store all of your cameras in your script (this solution is also better for perfomance, since both Camera.current and Camera.Main are not performant scripts).

In your case you will need to add this piece of code to your script and use the EnableSkyBoxCamera method.

public Camera defaultCamera;
public Camera skyBoxCamera;

private Camera _currentCamera;

public void EnableSkyBoxCamera(bool enableSkyBox)
{
    defaultCamera.enabled = !enableSkyBox;
    skyBoxCamera.enabled  = !enableSkyBox;

    if (enableSkyBox) _currentCamera = skyBoxCamera;
    else _currentCamera              = defaultCamera;
}
Jack Mariani
  • 2,270
  • 1
  • 15
  • 30
  • "since both Camera.current and Camera.Main are not performant scripts" Context is for kings. When it's only used when switching skyboxes that performance hit is utterly irrelevant by many orders of magnitude or more. – John Stock Apr 26 '23 at 17:50
0

If camera is null you can't set the enabled to false without getting a nullpointerexception. Instantiate the camera first or remove that line of code.

*** Edit ill take another crack at this

try

Camera.main.enabled = false; 

instead of

Camera.current.enabled = false;

As per Unity docs in reference to Camera.current: 'Most of the time you will want to use Camera.main instead. Use this function only when implementing one of the following events: MonoBehaviour.OnRenderImage, MonoBehaviour.OnPreRender, MonoBehaviour.OnPostRender'

vasmos
  • 2,472
  • 1
  • 10
  • 21
  • 1
    The Unity engine typically assigns an already-instantiated instance of `Camera` to `Camera.current`. This answer does not address the rhyme or reason as to why it would not be assigned. – Ruzihm Jul 29 '19 at 22:01
  • You might be right in most cases but in my case for example the player camera is not tagged as Main Camera so it will not work with Camera.main my guess is that I will have to add another public Camera variable and assign the camera I want to switch. – Daniel Lip Jul 29 '19 at 22:17
  • Camera.main will work only on a camera that is tagged as Main Camera – Daniel Lip Jul 29 '19 at 22:17
  • Better solution is to make a array or list of cameras I want to switch. – Daniel Lip Jul 29 '19 at 22:20
  • 1
    You can do that but my initial answer of removing the line Camera.current.enabled should technically fix your problem because you should never use it. :) You can reference your cameras and set them enabled and disabled all day as long as you don't have that line. – vasmos Jul 29 '19 at 22:24