• 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
1
Question by black_light · Jan 02, 2012 at 10:24 AM · componentmain camera

Accessing main camera componet

I have attached a component to my main camera that I want to access from a script. T$$anonymous$$s is the code I am using:

Camera.main.GetComponent<OrbitCamera>().FreeCam();

Where OrbitCamera is the script and FreeCam() is a function in it that I want to call but I get t$$anonymous$$s error:

Null Referance Exception: Object referance not set to an instance of an Object. Please Help.

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

3 Replies

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

Answer by black_light · Jan 02, 2012 at 11:15 AM

I just realized the error that shows on the bottom of the screen was from the previous compilation and that the code actually works(I am very new to unity and still getting used to the interface). Sorry for wasting everyone's time like t$$anonymous$$s.

Comment
Add comment · Show 1 · 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 Jace · Jan 02, 2012 at 11:17 AM 0
Share

Oh well, at least it's sorted - good luck!

avatar image
0

Answer by Jace · Jan 02, 2012 at 10:54 AM

Try

 OrbitCamera orbitCameraScript = Camera.main.GetComponent<OrbitCamera>();
 
 orbitCameraScript.FreeCam();
Comment
Add comment · Show 2 · 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 black_light · Jan 02, 2012 at 11:05 AM 0
Share

I have tried that and I get the same error

avatar image Jace · Jan 02, 2012 at 11:16 AM 0
Share

Apologies, I see your code is essentially the same. In which case I have to agree with @aldonaletto's logic and check what's actually null. i.e. is the camera tagged MainCamera and is the script actually there by that name

avatar image
0

Answer by aldonaletto · Jan 02, 2012 at 11:05 AM

The code is correct for C# (it should have a dot before <OrbitCamera> in JS).
Probably GetComponent can't find the script OrbitCamera, w$$anonymous$$ch produces t$$anonymous$$s error. Let's check the possibilities:
1- OrbitCamera is JS and your script is C# (or the other way around): JS and C# are compiled by different compilers, thus they can't see each other during compilation - but already compiled programs can be seen by any language, thus you can place OrbitCamera in one of the early compiled folders (Plugins, Standard Assets and Pro Standard Assets) and the code above in another Assets folder (Assets/Scripts or whatever).
2- The camera isn't tagged MainCamera (what would let Camera.main empty);
3- The script OrbitCamera is actually attached to a c$$anonymous$$ld object, or to another camera.

Comment
Add comment · Show 1 · 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 black_light · Jan 02, 2012 at 11:09 AM 0
Share

My orbit camera is js and script is in c# and my orbitcamera is placed in plugins and camera is tagged as main camera

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

6 People are following this question.

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

Related Questions

2D Animation does not start 1 Answer

How to make a dice fall when space bar is pressed. 3 Answers

Why wont this work? (I've done similar 1000 times fine) 1 Answer

Entity-Component-System 2 Answers

GetComponent of ALL clones? 2 Answers


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