- Home /
What are the errors here?
I want to create a script that creates two new cameras, position them by the main camera and the ultimate goal is to have each render to a different screen. I started the script but it gives me some errors. Here is the script.
function Start(){
var camera1 : Camera;
var camera2 : Camera;
var mainCam : Camera = Camera.main;
Instantiate(camera1, transform.position, transform.rotation);
Instantiate(camera2, transform.position, transform.rotation);
camX = mainCam.transform.position.x;
camera1.transform.Translate(Vector3(camX+1,0,0));
camera2.transform.Translate(Vector3(camX-1,0,0));
camera1.transform.parent = mainCam.transform;
camera2.transform.parent = mainCam.transform;
}
(updated code) I get "cannot convert UnityEngine.GameObject to UnityEngine.Camera".
Also, is it otherwise correct? This is the only obvious error I get.
Thanks in advance.
Answer by OperationDogBird · Aug 29, 2012 at 06:13 PM
The line that would throw this error is this
var mainCam : Camera = GameObject.FindGameObjectWithTag("Main Camera");
since GameObject.Find.. returns a GameObject. You should refer to it by using the exposed 'main' variable from the Camera class.
var mainCam:Camera=Camera.main;
Thanks! But now I get another error! "The object you are trying to instantiate is null". I try to change
var camera1 : Camera;
to
var camera1 : Camera.CopyFrom("mainCam");
but it does not recognize it!
I thought that maybe the new two cameras are without properties, so I wanted to copy everything from the main camera (which is the target purpose of the script)
Since you run the Camera.main line after you try instantiating (at least thats how you have it in the above code) then it would be null. Place that line before you try instantiating the new cameras and that should fix it.
No, I put the camera.main right after the first 2 lines (the sooner the better I thought :D)...still the same problem though. (updated code)
You can do this
var mainCam : Camera = Camera.main;
var cam1=Instantiate(mainCam.gameObject, transform.position, transform.rotation).camera;
var cam2=Instantiate(mainCam.gameObject, transform.position, transform.rotation).camera;
You will have the issue of '$$anonymous$$ore than one audio listener in the scene' if mainCam has the audio listener component.
Im not sure what you are trying to accomplish with your setup, but something to look out for. You can remove those components by saying
Destroy(cam1.GetComponent(AudioListener));
Destroy(cam2.GetComponent(AudioListener));
I followed your instruction. No errors, but Unity hangs on Play! Neither the game, nor the editor respond!
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Gun lag behind camera 1 Answer
How would I script camera sensitivity? 0 Answers
Increase camera Zoom 1 Answer
Network restrictive rendering 1 Answer