• 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 trinitysj96 · Jan 15, 2013 at 05:05 PM · cameracamera.main

Find Camera atuomatically

I need help with this code:

//var thetarget : Camera.main;

var cam : Camera = GameObject.Find("Main Camera");

function Update()

{ transform.LookAt(transform.position + cam.forward); }

The issue that I am having is that on my spawned enemies the camera is lost if I uncomment the first line and set the camera. but i want it to find the main camera automatically...

i get an error with the above in that : BCE0019: 'forward' is not a member of 'UnityEngine.Camera'.

does this make sense?

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 Fattie · Jan 15, 2013 at 05:08 PM

put this line up the top of your file

 private var theCam:Camera;

Next look at and click the actual camera in your Hierarchy panel.

You must rename the camera to happyCam

Finally, put this line as the first line in your Awake() routine

 theCam = GameObject.Find("happyCam").GetComponent(Camera);

Now you're all set. refer to the camera as "theCam"

Comment
Add comment · Show 4 · 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 trinitysj96 · Jan 15, 2013 at 11:01 PM 0
Share

did that : new code below

//var thetarget : Transform;

private var theCam:Camera;

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

function Update()

{ transform.LookAt(transform.position + theCam.forward); }

but i still get this error : BCE0019: 'forward' is not a member of 'UnityEngine.Camera'.

avatar image cdrandin · Jan 15, 2013 at 11:16 PM 1
Share

try transform.forward forward doesn't exist with camera component, only as a transform which forward is a vector3 with 1 unit in the blue axis.

avatar image trinitysj96 · Jan 15, 2013 at 11:26 PM 0
Share

that did it.. the solution was.

{ transform.LookAt(transform.position + theCam.transform.forward; }

avatar image trinitysj96 · Jan 16, 2013 at 04:42 PM 0
Share

thanks again it worked great.

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

C# Camera.main.GetComponent: Unknown Word SmoothFollow 0 Answers

How do I make the camera stop moving after a gameobject is destroyed? 1 Answer

Camera.main.position gives BCE0019 2 Answers

Can't use camera.main [SOLVED] 1 Answer

MainCamera just showing Skybox, when switching to Android 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