• 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
2
Question by MountDoomTeam · Nov 13, 2012 at 12:36 PM · cameragameobjectreferenceaccess

Access main camera from different GameObject script

I am making a scene editing utility, I have a GameObject w$$anonymous$$ch holds many textures that you can choose from to apply to prefabs, and I want to be able to place a line of 10 textured prefabs at the top of the screen using ViewportToWorldPoint in UnityScript.

I've tried everyt$$anonymous$$ng I can find on the forum but I can't reference the main active camera to find its coordinates, how do I reference it from the texture editing GameObject's script?

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
3
Best Answer

Answer by CodeMasterMike · Nov 13, 2012 at 12:48 PM

Look at these:

 Camera.current.ViewportToWorldPoint(Vector3.Zero);
 // or
 Camera.main.ViewportToWorldPoint(Vector3.Zero);

You can get the ViewportToWorldPoint information from two different ways in the Camera class:

Either with Camera.current or the Camera.main .

Read the documentation for the differences between those two.

Good luck!

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 MountDoomTeam · Nov 13, 2012 at 12:54 PM 0
Share

thanks! I can't get this to work in update function, it returns an error

  var p : Vector3 = Camera.current.ViewportToWorldPoint (Vector3 (1,1, camera.nearClipPlane));

same thing with Camera.main, it tells me

MissingComponentException: There is no 'Camera' attached to the "spawnpoint" game object, but a script is trying to access it. You probably need to add a Camera to the game object "spawnpoint". Or your script needs to check if the component is attached before using it. mazemaker2.Update () (at Assets/Scripts/mazemaker2.js:150)

it says I'm missing a component on the script, even though I am looking for a GameObject ? Camera is GameObject?

avatar image CodeMasterMike · Nov 13, 2012 at 01:14 PM 3
Share

@ZoomDomain

Camera and camera are two different things. Camera (with capital C) can be accessed from any script. While camera (with small c) is only accessable through a script that is attached to the camera object.

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

10 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

Related Questions

How do you reference different cameras 2 Answers

Referencing a Public GameObject from another script. 2 Answers

Obect1 to Object2 to Object1 again. Object1 points to Object2. Same Script for both objects that uses gameObject. Why?[Solved] 2 Answers

Accessing other GameObjects Script Variables 2 Answers

Can't re-enable a gameobject 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