• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Purpose2 · Jan 04, 2015 at 03:43 PM · canvasruntime

Unable to set the Canvas Render Camera at runtime

Hi, I'm trying to learn how to mess with all the new Unity UI stuff. I'm fairly new so there is a great chance I'm just being an idiot.

However, I don't seem to be able to set the main camera as the render camera for the canvas space after setting the rendermode to Screen Space Camera, below is a snippet of what I have... but I can't seem to find anything in the reference material for the syntax...

 theCanvasGO = new GameObject();
         theCanvasGO.name = "The Canvas";
         Canvas theCanvas = theCanvasGO.AddComponent<Canvas>();
         GraphicRaycaster theGraphicRaycaster = theCanvasGO.AddComponent<GraphicRaycaster>();
         RectTransform theCanvasRT = theCanvasGO.GetComponent<RectTransform>();
         theCanvas.renderMode = RenderMode.ScreenSpaceCamera;
         // here is where I'd like to assign the camera to the canvas...
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AcE_fLoOdEr · Jan 04, 2015 at 11:32 PM 0
Share

So in plain English, what are you trying to achieve?

avatar image Purpose2 · Jan 04, 2015 at 11:34 PM 0
Share

hi AcE_fLoOdEr,

Essentially, if you're adding it in Unity (and not via script) you're able to assign the main camera as the Render Camera, on the canvas. I want to be able to do this, at runtime for a canvas I am creating... however I cannot seem to find the correct syntax anywhere.

The ultimate goal however, is I want a canvas that is locked to the size of the screen of the player. Which I 'think' this is the right way to do this.

1 Reply

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by Eric5h5 · Jan 05, 2015 at 12:09 AM

Assign the camera to the worldCamera of the canvas.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Purpose2 · Jan 05, 2015 at 12:12 AM 0
Share

Ah god, that was it. I had actually already tried it. But I tried assigning it to "camera" as opposed to "Camera.main".

Doh.. thanks!

avatar image ABerlemont · Jun 03, 2016 at 03:26 PM 0
Share

This doesn't work for me (5.3.2f1)

avatar image JackofTraes ABerlemont · Oct 13, 2016 at 05:30 PM 0
Share

This method is now depreciated which means you will need to use a slightly different syntax. For this example, the fix would be something like: theCanvas.worldCamera = Camera.main.GetComponent();

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Export objects to a .3DS file at runtime 1 Answer

Multiple Canvases, paste/skin them to other objects in a single scene 0 Answers

Canvas Text renders under Image 1 Answer

Is it currently possible to do a complete Mecanim setup via script? 1 Answer

Load object at runtime using webplayer 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges