Transform ---> Null Reference expection in assignmet weird weird

Hi Guys,

how is possible that I declared a variable

private var x: Transform;

and in the Start function I call:

x.position = Vector3(0,0,0)

and I get a NullReference Expection!!?

Thanks

To make sure that you are have x assigned, right before x.position=… put print(x);

If it prints null in the log, it is not referenced, thus causing your error.

Not 100% sure of terminology.

X is a variable that refers to a specific instance of transform.

You haven’t set a transform, therefor its current value is null.

Normally you would do something like this:

 private var x : Transform = new Transform();

but I am not 100% sure that in this case it is possible (first thing i’d try of course).

Guys, this is the code:

function SetPositionForDesign() {
	tmpObjSettings.position = transform.position;
	tmpObjSettings.rotation = transform.rotation;
	transform.position = tmp_pos.position;
	transform.rotation = tmp_pos.rotation;
}

this function is called in the OnMouseDown() in a script attached to the object. The error is returned me on the first assignment. I verified that transform is not null. So… :frowning:

Thanks for any help.

tmpObjSettings could be null, could you post the whole code and not only a part of it?

Well, that’s the whole code, it’s in the function and the variable is also called in the

function Start() {
var gg: GameObject = GameObject.Find("i_Camera");
cam = gg.camera;
 
tmp_pos = GameObject.Find("i_objTarget").transform;
tmpObjSettings.position = Vector3(0,0,0);
tmpObjSettings.rotation = Quaternion.identity;
}

and yes tmpObjSettings is null so what? Null is a value and postion/rotation are not read only ones, I hope. :slight_smile:

When tmpObjSettingsis null why do you expect that this code will work without an error?

You try to assign position of something not existing, this produces a nullreference exception.

Well,

how am I supposed to save the position of a game Object before modifying it? Secondly, I am using tmpObjSetting it’s a variable, declared Private Var, and that it’s an initialization. So, if I need to apply a transform to an object, what am I supposed to do?

You said its null, now you tell me its initialized (not null). So what is correct?

Or better: What are you trying to do? And why dont you just post the whole script and not only the Start function? This will make helping much easier.

Mark, I really appreciate your help, really but are you reading my post carefully?
I’ve put the initialization in the function Start(), which is (for me) where I want to inizialize an arbitrary transform. What I would like to do is simply moving the object to a position, and putting the object back to the previous one. The tmpObjSetting is supposed to be a dummy variable which stores the previous/current position, then I move the object, than I put the object back. As simple as that. So, why on … I can’t assign the current transform of a gameObject to a variable?

Well, if this is everything then tmpObjSettings will always be null, a null reference, you cant access a nullreference by specifying a property of the theoretical instance, you cant take A from Nothing.

What you need to do is to ensure tmpObjSettings contains a valid instance, either by creating a new Transform() before using it (like NPSF3000 already mentioned) or by completely ignoring tmpObjSettings and storing everything separat:

var tmpPosition = Vector3(0,0,0);
var tmpRotation = Quaternion.identity;

And to “are you reading my post carefully?” I tried, but you didnt make it very easy to understand why you are stuck to the problem after several attempts to help you.

A thing I don’t understand is why Unity treats transform (or other objects) differently from standard variables? to overcome the problem I created

private var tmpPos : Vector3;
private var tmpRot: Quaternion;

and then before moving the object I saved
tmpPos = transform.position;
tmpRot = transform.rotation;
MoveTheObject();

then, I have another event which requires the object back to its original position. That’s it. Sorry, if I sounded nervous, but sometimes I have the feeling that Unity (or may be Javascript)… is way far from the standard programming basics. Anyway…of course I really appreciate the helps… :slight_smile:

Its not a Unity problem, the problem is related to the type of the instance:

reference type

vs

value type

reference types can be null
value types will at least be the default value

http://www.albahari.com/valuevsreftypes.aspx

Transform is an OBJECT not a data type. You obviously don’t have any experience in OOP.

In order to access the data members or methods of an object, it must first be initialized (Initialization != declaration).

Example of declaration:
var tmpObjectSettings : Transform

Example of initialization:
tmpObjectSettings = new Transform();

put the former at the top of the script and the latter inside of the start() function and see what happens.

Well… “You obviously don’t have any experience in OOP.”, next time it would be greatly appreciated if you can keep your judgement for yourself…

secondly, Transform is a datatype as you are allowed in Javascript to declare

var x: Transform as you actually stated.

and do whatever you like. Rather x is a pointer or an object, doesn’t matter. Transform is a datatype as it is a Class! Exactly like int, boolean and the rest.

third, I did the New operator assignment but it didn’t work either.

and just to be complete if you call

var x: Transform;
x = new Transform ();

this is what unity returns:
Assets/Standard Assets/iScripts/i_SelectObject.js(31,22): BCE0120: ‘UnityEngine.Transform.constructor’ is inaccessible due to its protection level.

I apologize, I wasn’t trying to be rude, it just came off that way.

Anyway, you are right, I used hte wrong terminology; my mistake. What I meant to say was that Objects are not primitives.

In C#, all objects are declared as pointers (Dynamic memory). Because of this, you need to initialize the object before you can use it or any of its data.

Not really, value types (all structs) exist without the need to first initialize them, the math classes (Vector, Quaternion, eg.) in Unity and almost all primitive types (except string) are such a case.

Not entirely true… They need to be initialized, BUT in Unity’s case, with is COP nature, you can initialize them by assigning them values via the inspector. In most cases, the actual initialization is done behind the scenes, and I almost stated that it was rare to actually need to manually initialize one of unity’s classes, but I decided against it as I wanted him to understand the OOP paradigm and why his way didn’t work.

Well value types always have a default value, they cant be null. Transform and all other classes are reference types, which is a different thing :wink:

But I see what you mean and we should stop our little intermezzo in this thread :wink: