- Home /
Problem getting GetComponent to work
hello, i just had a quick question. i am trying to access a variable from a script attached to my main camera here is the code.
Camera.main.GetComponent<camera>().target = myship.transform;
i have tried multiple different ways and it still comes up with this error.
Assets/Scripts/NetworkManager.cs(25,42): error CS0246: The type or namespace name `camera' could not be found. Are you missing a using directive or an assembly reference?
can someone tell me what i am doing wrong.
thank you, mewhy
Is your script named "camera"? The name of the script needs to be inside the angle brackets.
If it's the unity Camera component you're trying to access, it has a capital 'C'.
Answer by Kronos312 · Mar 04, 2013 at 07:32 AM
try this
if u have a gameobject named "Camera" and script named "camera"
GameObject MyCamera = GameObject.Find ("Camera"); //fetch the object
if (MyCamera==null) print ("Could not find the object Camera");
camera cameraScript = MyCamera.GetComponent<camera>(); // this is mainly to check what is your problem
if (camera==null) print ("Cant find my script camera"); // use this line OR the next line
MyCamera.GetComponent<camera>().target = myship.transform; // or use this line this is your line
// camera.target = myship.transform // if you have the script like here in this exmaple.
after this you should be able to understand where is your problem. good luck.
Thanks $$anonymous$$ronos312, the only problem is it comes up with the compile error Before i can print anything to the console. it is kinda weird. do you think you could give me a JS version of this so i can see if that is the problem?
Ok I converted this into JS just to test it and my problem is that it cannot find the script named camera. could someone help me out?
Answer by mewhy · Mar 04, 2013 at 06:08 PM
HEY I GOT IT!!!! read this and you will see what my problem was. http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613093756im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity cannot find component of an object in an array. 2 Answers
can't create new project , can't find component 0 Answers
Object Reference not set to instance of object 2 Answers
An instance of type 'UnityEngine.Animator' is required to access non static member 'GetBool' 1 Answer
Modifying components of an array 1 Answer