• 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 SpeedTutor · Oct 09, 2013 at 11:55 AM · javascriptmaincamera

Maincamera from another script

Hello, I'm trying to access the main camera, but my script is placed on my FPSController. Everytime I try and press "4" to do something with the camera it says:

 MissingComponentException: There is no 'Camera' attached to the "First Person Controller" game object, but a script is trying to access it.
 SPController.Update () (at Assets/Scripts/SPController.js:88)

I'm not sure what else to try and do, I've added the maincamera to the inspector slot. I'm not sure if I've missed something obvious.

 var mainCamera : Camera;
 var zoom : int = 20;
 var normal : int = 60;
 var smooth : float = 5;
 
 if(Input.GetKeyDown("4"))
     {
         hawkEye = !hawkEye;
       
           if(hawkEye == true)
           {
               mainCamera.fieldOfView = Mathf.Lerp(camera.fieldOfView, zoom, Time.deltaTime * smooth);
           }
           
           else if(hawkEye == false)
           {
               mainCamera.fieldOfView = Mathf.Lerp(camera.fieldOfView, normal, Time.deltaTime * smooth);
           }
       }
Comment
Add comment · Show 5
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 KiraSensei · Oct 09, 2013 at 12:03 PM 0
Share

Is mainCamera valuated in the inspector ? Or do you give it a value somewhere else ? (in the Start function, or Awake for example)

By the way, you can simplify a little bit your code like :

 if (Input.Get$$anonymous$$eyDown("4"))
 {
     hawkEye = !hawkEye;

     if(hawkEye)
     {
         mainCamera.fieldOfView = $$anonymous$$athf.Lerp(camera.fieldOfView, zoom, Time.deltaTime * smooth);
     }
     else
     {
         mainCamera.fieldOfView = $$anonymous$$athf.Lerp(camera.fieldOfView, normal, Time.deltaTime * smooth);
     }
 }
avatar image SpeedTutor · Oct 09, 2013 at 12:07 PM 0
Share

I only dragged the $$anonymous$$ainCamera into the appropriate inspector slot, and it outputs correctly. I did also try within the start function:

 mainCamera = GameObject.Find("$$anonymous$$ain Camera").GetComponent(Camera);

It placed it automatically in the slot for me, but have the same issue. $$anonymous$$aybe I'm not grasping what you're asking.

avatar image SpeedTutor · Oct 09, 2013 at 12:11 PM 0
Share

Ah yes, I get you. There's always something stupid that catches me out. Thanks for pointing it out.

avatar image vexe · Oct 09, 2013 at 12:11 PM 0
Share

Did that solve your problem? Should I move my comment to an answer?

avatar image SpeedTutor · Oct 09, 2013 at 12:15 PM 0
Share

Yes, go for it.

2 Replies

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

Answer by vexe · Oct 09, 2013 at 12:09 PM

You have a reference to a camera, called mainCamera which you're assigning from the inspector, everything's fine there. But you're trying to access the camera component that's attached to the gameObject that this script is attached to (by calling camera), but it's not finding it cause there isn't any. In other words, your problem isn't with mainCamera, but with camera. (`camera` always refers to the camera component of your gameObject)

Did you mean to use mainCamera instead of camera?

 mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, normal, Time.deltaTime * smooth);

Btw your could could be drastically simplified:

 mainCamera.fieldOfView = Mathf.Lerp(camera.fieldOfView, hawkEye ? zoom : nomral, Time.deltaTime * smooth);

And btw, if by mainCamera you mean the main camera in your scene, why not just access it via Camera.main? - Just make sure the camera has the MainCamera tag for this to work.

Comment
Add comment · 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
1

Answer by Tomer-Barkan · Oct 09, 2013 at 12:13 PM

You're trying to access camera.fieldOfView within the script, but the script is not attached to a camera object, so the camera variable does not exist.

Instead, you might have meant to use mainCamera.fieldOfView:

 var mainCamera : Camera;
 var zoom : int = 20;
 var normal : int = 60;
 var smooth : float = 5;

 if(Input.GetKeyDown("4"))
 {
     hawkEye = !hawkEye;

     if(hawkEye == true)
     {
         mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, zoom, Time.deltaTime * smooth);
     }

     else if(hawkEye == false)
     {
         mainCamera.fieldOfView = Mathf.Lerp(mainCamera.fieldOfView, normal, Time.deltaTime * smooth);
     }
 }
Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

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

Related Questions

Setting Scroll View Width GUILayout 1 Answer

How can I zoom out my Field of View slowly? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Problem with rotation of objects 0 Answers

How to code a script that points you to the previous scene (JS) 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