x


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.

more ▼

asked Sep 30 '11 at 04:45 PM

zachypin gravatar image

zachypin
71 35 38 38

Instead of:

<PlayerCamera>();

use

<PlayerCamera>.();

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

Sep 30 '11 at 05:51 PM SilverTabby

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

Sep 30 '11 at 06:20 PM zachypin

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 MightyGoob 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.

Sep 30 '11 at 08:51 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Sep 30 '11 at 07:28 PM

J3 Gaming gravatar image

J3 Gaming
244 17 21 24

Sep 30 '11 at 08:52 PM Bunny83

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

Sep 30 '11 at 09:02 PM J3 Gaming
(comments are locked)
10|3000 characters needed characters left

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

PlayerCamera cameraScript = camera.GetComponent<PlayerCamera>();
more ▼

answered Sep 30 '11 at 04:59 PM

MattMaker gravatar image

MattMaker
71 1 3

I edited the question with your fix.

Sep 30 '11 at 05:24 PM zachypin

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

Sep 30 '11 at 05:52 PM SilverTabby
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5051
x4137
x3316
x2071
x404

asked: Sep 30 '11 at 04:45 PM

Seen: 4024 times

Last Updated: Sep 30 '11 at 11:49 PM