- Home /
Assign the camera to a variable in a prefab
I want to assign the camera that is an object in the scene to a variable in a script attached to a prefab but it won't allow the camera to attach, why is this? Could someone give me an alternative?
You can't assign a object from the scene to a prefab in memory. What would happen if the scene changes? What are you trying to do?
Prefabs are Templates - they can only reference other Prefabs. Imagine you could reference Objects in the scene, but then dragging the Prefab into another Scene. Objects in the scene can reference Prefabs, but not the other way round.
What exactly do you want to do?
Is the variable in the script of type Camera? If you only have one Camera and it's tagged "$$anonymous$$ain Camera" (the default) you could do:
private Camera mainCam;
and in Start() do
mainCam = Camera.main
or FindObjectWithTag("$$anonymous$$ain Camera")
(untested).
Thanks for posting this! Worked for my scenario, and learned from it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613130058im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to assign a instantiated gameobject to other script of gameobject 1 Answer
Why is Unity deleting my prefabs!? 1 Answer
2D Border doesnt allow the nutbomb gameobject to bounce 0 Answers
Object reference not set to an instance of an object 0 Answers
camera movments fixed.. character controller without using character controller -.-' 2 Answers