- Home /
Getting a camera at runtime
I'm not sure if i'v phrased the question right. what I need to do is set an object (camera) in the inspector at runtime similar to this
var me : GameObject;
function Start() { if (me == null) { me = GameObject.FindWithTag("player1"); }
this returns the game object in the scene and sets it in the inspector of my script. I'm trying to replicate the same thing but returning a camera instead. this is what i have so far.
var deathCam : Camera;
function Start() { if (deathCam == null) { GameObject.Find ("MainCamera"); }
i put debug logs in and they all get set off so my script is running through fine however its not setting the camera in the inspector. What am i doing wrong?
Answer by Jessy · Feb 23, 2011 at 09:38 PM
You're not assigning anything.
if (!deathCam) deathCam = GameObject.Find("MainCamera").camera;
Probably not really much of a point in that, though.
http://unity3d.com/support/documentation/ScriptReference/Camera-main.html
If you want performance, cache it, using that concise code.
if (!deathCam) deathCam = Camera.main;
I had to use the top piece of code with if(deathCam == null) It needs to be assinged via script in the inspector at runtime, forgive me if your jargon doesn't match $$anonymous$$e. The player already has a camera, when the player dies they need to switch off their camera and switch on the main one, this needs to happen automatically for all our levels and this is the best way I could think of doing it. The bottom part of code I couldn't get working, all though performance is obviously a priority the top method works fine so thnak you for your answer