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

How to use Canvas.worldCamera

Discussion in '2D' started by Fe0dor, Mar 26, 2020.

  1. Fe0dor

    Fe0dor

    Joined:
    Jan 18, 2020
    Posts:
    17
    Hi,

    so I am working on a project where I use a Canvas as a Prefab.
    I want to use the Screen Space - Camera and I have to reference my Camera as Render Camera per script since it is a Prefab.

    There were multiple people on the Internet saying I could use Canvas.worldCamera but it is not working or being seen as a viable Method by my Compiler either. (I am using Visual Studio)

    Could anyone of you tell me how I could include Canvas.worldCamera in the following script?
    that would be really nice
    Code (CSharp):
    1. public class Canvas : MonoBehaviour
    2. {
    3.     // Start is called before the first frame update
    4.  
    5.     Camera camera;
    6.     Canvas canvas;
    7.    
    8.     void Start()
    9.     {
    10.         camera = Camera.main;
    11.        
    12.        
    13.        
    14.     }
    15. }
     
  2. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    Its a setting on Canvas, a value under Render Mode. Change your 'main' canvas settings there.
     
  3. Fe0dor

    Fe0dor

    Joined:
    Jan 18, 2020
    Posts:
    17
    I know I can reference my Camera that way. But in a Prefab this does not work so I have to do it inside a Script.
    What Im asking for is a way to use Canvas.worldCamera because it doesnt work if I use it inside my script.
     
  4. Primoz56

    Primoz56

    Joined:
    May 9, 2018
    Posts:
    369
    Prefabs will not save anything outside of their own scope, so reference the camera or create a reference to it after creating the prefab. Haven't played enough with worldcamera myself but it should work as intended.

    Alternatively create a static object with a fixed reference to your camera (eg. GameManager.GetCamera). Not optimal but a viable workaround.