- Home /
UnityScript error in transform.Position
Hi, This has probably been asked a million times already but i cant seem to find the answer anywhere. I am trying to write a script so that my camera moves to a new angle when a button is pressed, however i keep getting the error :"An instance of type 'UnitEngine.Component is required to access non static member 'transform'. I get this error with both the rotation and position of the camera. This is my code:
var camera = GameObject.Find("Camera");
function Update() {
if (Input.GetKeyDown(KeyCode.D))
{
Camera.transform.Position = Vector3(0,11,17.78);
Camera.transform.Rotation = Vector3(0,180,40);
}
}
Hope anyone can help me with htis and thanks in advance :)
Answer by Posly · Feb 21, 2013 at 07:26 PM
Hm first I would attach this code right on to the camera, so you dont have to use "var camera = GameObject..." instead you can just use "transform.position". Everything in line 5 looks okay you just have to make the p in position lowercase. As for line 6 try using "EulerAngles" instead of Rotation. Hope this helps!
function Update () { if(Input.GetKeyDown(KeyCode.D)) { transform.position = Vector3(0, 11, 17.78); transform.EulerAngles = Vector3(0, 180, 40); } }
Thanks for the quick response, this was really helpful and i got it fixed now. :) EDIT: The camera is now moving to the correct position but does not seem to rotate, even if i use "EulerAngles". Would you be so kind to take a look at my new code? Thanks in advance.
function Update() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D))
{
transform.position = Vector3(0,11,17.78);
transform.EulerAngles = Vector3(0,0,0);
}
}
Answer by Eric5h5 · Feb 21, 2013 at 07:45 PM
You need to pay attention to spelling and capitalization. You have a variable called "camera" but you're referring to "Camera", which are two different things. If you look in the docs, you can see that there are no properties of Transform called "Rotation" or "Position". (Also, Transform.rotation is a quaternion and does not take a Vector3.)
Your answer
Follow this Question
Related Questions
Gun Script Error (Fixed!!!) 1 Answer
Expand script to stop camera from following player 2 Answers
Error Java script 1 Answer
transform.parent can't go with gameObject.find 3 Answers
Facebook Hash key error 0 Answers