Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Current camera name

Discussion in 'Scripting' started by tomek771234, May 4, 2019.

  1. tomek771234

    tomek771234

    Joined:
    Mar 29, 2019
    Posts:
    2
    Dear All,
    I'm new to C# and I've tried to access the name of current camera, and I got the name using:
    Camera.current.gameObject.name or Camera.current.name.
    The problem is that I also got message in console:
    NullReferenceException: Object reference not set to an instance of an object
    GameTimer.Update ().
    I do not understand why, since the value is printed with:
    Debug.Log(Camera.current.name);
    Could you please tell me what to do to avoid this error?
     
  2. schneckerstein

    schneckerstein

    Joined:
    Dec 6, 2018
    Posts:
    36
  3. tomek771234

    tomek771234

    Joined:
    Mar 29, 2019
    Posts:
    2
    Thank you. Yes I've read it.
    I'm using three cameras (Main + two other Cameras).
    I'm switching between them, instead of changing scenes.
    On those two other cameras I show Canvas and the Main Camera shows main game in 3D.
    I want to check which one is currently used as I want to use timer only when showing this camera.
    That's why I need Camera.current not Camera.main.
    But maybe there is a different solution, which I do not know for my problem.
     
  4. schneckerstein

    schneckerstein

    Joined:
    Dec 6, 2018
    Posts:
    36
    Sounds like a job for layers. If you place the UI on a extra layer, you can ignore this layer on your two other cameras.