Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by zachypin · Sep 30, 2011 at 04:45 PM · c#gameobjectgetcomponent

C# GetComponent Issue

 GameObject camera = gameObject.Find("PlayerCamera");
 PlayerCamera cameraScript = camera.GetComponent<PlayerCamera>();
 cameraScript.snapToBack();

I don't understand why this isn't compiling -_- The compiler says "type or namespace PlayerCamera cannot be found" on the PlayerCamera cameraScript line.

Comment
Add comment · Show 3
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 SilverTabby · Sep 30, 2011 at 05:51 PM 0
Share

Ins$$anonymous$$d of:

 <PlayerCamera>();

use

 <PlayerCamera>.();

I don't know why the dot is needed either, but it seems to work for me

avatar image zachypin · Sep 30, 2011 at 06:20 PM 0
Share

hmm that doesn't seem to work. the error I get now is "Unexpected symbol '(', expecting 'identifier'"

avatar image Bunny83 · Sep 30, 2011 at 08:51 PM 0
Share

That doesn't make much sense. I guess you meant the UnityScript syntax for generic parameters but the dot have to go in front of the generic brackets, not behind them.

 // C#
 PlayerCamera cameraScript = camera.GetComponent<PlayerCamera>();
 
 // UnityScript
 var cameraScript = camera.GetComponent.<PlayerCamera>();


Besides that the question title clearly states C# as language so the syntax it absolutely right. The compiler just can't find the type PlayerCamera so it's not there, is misspelled or as $$anonymous$$ightyGoob said if PlayerCamera is not a C# script you have to put it in a compilation group above the script from which you are using it.

2 Replies

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

Answer by J3-Gaming · Sep 30, 2011 at 07:28 PM

If the PlayerCamera script is javascript, you will need to put it in the correct file directory.

If PlayerCamera is C#, put it in the same folder as this script.

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 Bunny83 · Sep 30, 2011 at 08:52 PM 0
Share

Here's the documentation about compilation order

avatar image J3-Gaming · Sep 30, 2011 at 09:02 PM 2
Share

This one is a better solution (I$$anonymous$$O) before Unity added the compile order: http://www.chrisdanielson.com/2011/04/22/unity-3d-javascript-and-c-scopingcompiling-issue/

avatar image
0

Answer by MattMaker · Sep 30, 2011 at 04:59 PM

Don't put quotes in the type specifier; you meant this:

 PlayerCamera cameraScript = camera.GetComponent<PlayerCamera>();
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 zachypin · Sep 30, 2011 at 05:24 PM 0
Share

I edited the question with your fix.

avatar image SilverTabby · Sep 30, 2011 at 05:52 PM 0
Share

Try to avoid posting comments as answers please: it messes with the search system and can give the false impression that the question was already answered

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.

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

How to get a variable value from another script(C#)? 1 Answer

Accessing a value in a script attached to another game object 3 Answers

Refering to gameobject script is attached to 1 Answer

C# GetComponent / change values throught other script 3 Answers

Two exact objects behaving differently... *scratches head* 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