Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by mechanicarts · Aug 29, 2012 at 06:06 PM · cameradual

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mechanicarts · Aug 29, 2012 at 08:15 PM 0
Share

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)

avatar image OperationDogBird · Aug 29, 2012 at 11:48 PM 0
Share

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.

avatar image mechanicarts · Aug 30, 2012 at 08:15 AM 0
Share

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)

avatar image OperationDogBird · Aug 30, 2012 at 08:26 AM 0
Share

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));
avatar image mechanicarts · Aug 30, 2012 at 11:11 PM 0
Share

I followed your instruction. No errors, but Unity hangs on Play! Neither the game, nor the editor respond!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges