transform.position error


I'm trying to get the position of an object with..

var objectpos : Vector3 = transform.position;

but I get the error "Argument Exception: You are not allowed to call get_transform when declaring a variable. Move it to the line after without a variable decleration."

I dont really understand how to solve this, can anyone help?


more ▼

asked Jan 13, 2011 at 04:40 PM

keefus gravatar image

47 15 14 20

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I assume you're putting this in the start of your code? I've run into this error before and it really shouldn't affect anything, but you can easily get around it by switching your code into two lines.

var objectpos : Vector3;
objectpos = transform.position;


more ▼

answered Jan 13, 2011 at 04:46 PM

Bob5602 gravatar image

603 40 33 50

Also, depending on what you're doing, you may want to declare the objectpos variable as an open variable in your quote, then make the objectpos = transform.position; code in the update() function if you want it to be current, or in the Start() function if you just want it to be the original object position.

Jan 13, 2011 at 04:49 PM Bob5602

Thanks mate, thats nailed it.

appreciate it.

Jan 13, 2011 at 04:51 PM keefus

Thats great, thanks for the additional info.

Just as an aside, would you happen to know the correct syntax for getting the position of a different gameobject to the one with the script attatched? another simple thing I'm struggling with. Thanks in advance.

Jan 13, 2011 at 05:04 PM keefus

You need to declare the other gameobject, check http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html . I'd do something like var otherObject : GameObject;, then otherObject = GameObject.Find("name"); and then you can use otherObject.transform.position to get its position

Jan 13, 2011 at 05:35 PM Bob5602

Thanks very much for helping me out.

I am however having an issue with the line "otherObject.transform.position" I get the error "Cannot convert 'UnityEngine.Vector3' to 'UnityEngine.GameObject'"

Jan 14, 2011 at 09:54 AM keefus
(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



Answers and Comments



asked: Jan 13, 2011 at 04:40 PM

Seen: 1953 times

Last Updated: Jan 13, 2011 at 04:40 PM