- Home /
Can I attach the Main Camera to two scripts?
I have objects Main Camera and Player Ship. Player Ship has a script attached (Ship.js) with a var myCamera : Camera (Main Camera is assigned).
Now I've created another script HitPointBarController.js, also with a var myCamera : Camera, but the Inspector won't let me assign Main Camera to it.
What's up with that? Am I missing something?
Answer by Giannis7312777 · Jan 04, 2014 at 06:30 PM
A another way is to search for the tag of camera on function Start. Here's an example:
var myCamera : Camera;
function Start(){
myCamera = GameObject.FindGameObjectWithTag("MainCamera");
}
Hope it helped :)
@Giannis: I tried your suggestion and got this error:
Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Camera'.
Ah, your answer led me to find the correct code. If you add a .camera to line 4, I'll mark it as the answer.
you dont have to set a var for $$anonymous$$ain Camera you can do it like:
Camera.main.backgroundColor = Color.red;
Camera.main.ReadToLearn
Your answer
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Create a script to disable local gameobject and enable another gameobject in the scene 1 Answer
Camera do not work as a prefab 0 Answers
How to assign a instantiated gameobject to other script of gameobject 1 Answer
AddForce to Camera 1 Answer