- Home /
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)
Answer by SarperS · Jun 22, 2012 at 07:40 PM
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.
Answer by Wolfram · Jun 22, 2012 at 08:20 PM
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