- Home /
Setting Canvas EventCamera (worldCamera) component
I have been stumped for a few days now on this, figured this would be a quick script but its consuming a lot of time. I'm attempting to set the UI canvas WorldSpace Event camera component from a script. The camera is in World space mode of course, but when my prefab instantiates it doesn't have a event camera set. So of course setting it is my main priority so i can click on the object.
Help would be greatly appreciated.
using UnityEngine;
using System.Collections;
public class SetCanvasCamera : MonoBehaviour {
public GameObject MyCamera;
public GameObject MyCanvas;
// Use this for initialization
void Start () {
MyCamera = GameObject.FindWithTag("MainCamera");
Canvas theCanvas = MyCanvas.GetComponent<Canvas>();
theCanvas.worldCamera =MyCamera;
}
}
I have also looked around online to find alternatives but unfortunately i could not find a way to solve this issue. If by any chance its just me being blind, it would be awesome if someone could point that out.
Answer by MRPoof · Jan 12, 2015 at 09:00 AM
Found a different and much more effective way to resolve the issue. One Canvas, buttons stored already in scene then find there transform when its instantiated and all left over are destroyed. Little bit more work, but more efficient then having a hundred plus canvases. The difference was 20 fps and 240 fps! So ya'll have a good night/Day/evening/morning.
It is THAN, NOT THEN. ALL UNITY USERS CAN"T SPEAK PROPER ENGLISH OR WHAT !?
Hello Lumbert,
Please refrain from commenting on posts from over 5 years ago with unhelpful suggestions. If you have suggestions for code-related things, then they are highly appreciated. However, comments unrelated to the question or answer are deemed unhelpful, especially on 5+ year-old posts that have an accepted answer. Please refrain from doing so in the future as following many of your posts through your account you've made quite a number of spelling and grammatical mistakes as well. We are to practice what we preach.
Thank you, have a nice day.
Answer by kwin · Jan 22, 2015 at 09:30 AM
Please try,
public GameObject MyCamera; ---> public Camera MyCamera;
MyCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera> ();
Your answer
![](https://koobas.hobune.stream/wayback/20220613180042im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UI Slider; -x to +x range, 0 center; how can I get it to fill both directions starting from center? 3 Answers
Extending unity component and Adding listener? 1 Answer
2D Animation does not start 1 Answer
Button interactable not working via script 1 Answer
What do the dotted-square button and R button on a Rect Transform do? 2 Answers