x


Setting position of a transform?

Hello im trying to set the position of a transform i can do this with vectors but i want to use a transform because later i will need to get/set the rotation to.

What i did

public Transform[] cameraPos = new Transform[10];
public Vector3[] camPos = new Vector3[10];

// Use this for initialization
void Start () {

//this works

    camPos[0] = new Vector3(13.6f, -3.1f, 3.3f);

//this dosent work

    cameraPos[0].position = new Vector3(-1.4f, -3.1f, 3.3f);
}

Also here is the error im gettting

UnassignedReferenceException: The variable cameraPos of 'CameraPosition' has not been assigned.
You probably need to assign the cameraPos variable of the CameraPosition script in the inspector.
UnityEngine.Transform.set_position (Vector3 value)
CameraPosition.Start () (at Assets/ModelViewer/CameraPosition.cs:16)
more ▼

asked Jun 22 '12 at 06:47 PM

edrocks4u gravatar image

edrocks4u
36 1 2 3

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

2 answers: sort voted first

Internally, Vector3 can be seen a struct, which essentially means you can directly use it, assign stuff to it, and it will never be null. This is, from a programmers perspective, not entirely accurate, but it helps for noticing the inherent difference between this kind of object, and the other category of objects:

Almost all other objects in Unity are classes. They work differenty, they can be arbitrarily complex, but most importantly, they don't work "just like that". You'll first have to create or assign an object to them. They are only references to objects. They can be null.

For example, this says "I am a vector, use me":

Vector3 myVec;

But this is just "look, I can be an object, but you need to show me which one":

GameObject myGameObject;
Component myComponent;
Transform myTransform;

For these objects, you first need to assign them something. You can do that in the inspector. Or for example create new ones:

myGameObject=new GameObject("I am a new object");
myComponent=myGameOBject.AddComponent<MeshRenderer>();
myTransform=myGameObject.transform; // when creating a new GameObject, it will already have a transform
more ▼

answered Jun 22 '12 at 08:20 PM

Wolfram gravatar image

Wolfram
9k 8 20 52

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

You are trying to give a value to a null array field. Assign the transform you want to position into the cameraPos array's 0th field in the inspector.

more ▼

answered Jun 22 '12 at 07:40 PM

Sarper Soher gravatar image

Sarper Soher
1.6k 1 4 16

(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:

x4147
x1359
x1278
x575

asked: Jun 22 '12 at 06:47 PM

Seen: 323 times

Last Updated: Jun 22 '12 at 08:22 PM