- Home /
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.
Ins$$anonymous$$d of:
<PlayerCamera>();
use
<PlayerCamera>.();
I don't know why the dot is needed either, but it seems to work for me
hmm that doesn't seem to work. the error I get now is "Unexpected symbol '(', expecting 'identifier'"
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.
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.
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/
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>();
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
Follow this Question
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