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 /
This question was closed Mar 04, 2013 at 04:14 AM by sona.viswam for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by sona.viswam · Feb 14, 2013 at 11:39 AM · c#camerascreenpointtoray

how to access camera from another object

My script is attached to a character controller. And i declared

 public Camera camera;

in class. In update function i given

 if(Input.GetMouseButtonDown(0)){ 
 Ray ray = camera.ScreenPointToRay(Input.mousePosition);    
 if (Physics.Raycast (ray, out hit3, 400.0F)){
     print(hit3.collider.gameObject.name); 
 }
 }

But i am getting error

 "UnassignedReferenceException: 
 The variable camera of 'characterScript' has not been assigned.
 You probably need to assign the camera variable of the 
 characterScript script in the inspector."

Help me with a good solution. Thanks in Advance

Comment
Add comment · Show 4
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 theUndeadEmo · Feb 14, 2013 at 11:45 AM 0
Share
 Ray ray = camera.ScreenPointToRay(Input.mousePosition); 
 
 //to
 
 Ray ray = cam.ScreenPointToRay(Input.mousePosition); 
 
 
avatar image theUndeadEmo · Feb 14, 2013 at 11:47 AM 0
Share

you changed you question as before you had cam = GameObject.Find ......

now drag the camera component into the public you defined. see if that works

avatar image sona.viswam · Feb 14, 2013 at 11:48 AM 0
Share

i tried. its showing error: Assets/Scripts/characterScript.cs(52,39): error CS1061: Type UnityEngine.GameObject' does not contain a definition for ScreenPointToRay' and no extension method ScreenPointToRay' of type UnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

avatar image robertbu · Feb 14, 2013 at 02:52 PM 0
Share

Note that Camera.main is access to the camera named "$$anonymous$$ain Camera" so you can do something like:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by OmarAlhaddad · Feb 14, 2013 at 03:23 PM

you can use:

 Camera.mainCamera

But make sure your camera has the "MainCamera" Tag

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 Srbhunter · Feb 25, 2014 at 12:12 AM 0
Share

Thanks man!

Follow this Question

Answers Answers and Comments

12 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

Related Questions

C# FP_Camera not following Character Controller 0 Answers

C# locking Main Camera's Rotations 1 Answer

No Glitch Please ... 1 Answer

capture image on phone and use it as background of the scene 0 Answers

Disabling a child script within the parent. 0 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