- Home /
Question is off-topic or not relevant
NullReferenceException by localRotation And Quaternion.Euler!??
I'm having A Trouble In Converting From JavaScript To C#, I Converted It And Until Now No Errors, But When I Entered Playmode, It Gave Me NullReferenceException Error, And Not 1 Line Only! Each Lines That Contain:
TRANSFORMNAME.localRotation = Quaternion.Euler(blah,blah,blah)
I Get:
NullReferenceException: Object reference not set to an instance of an object
And The Line That I Show You Now Is An Example Of The Error:
public Transform needle;
public float value = 0f;
public float maxValue = 100f;
if(needle)
{
//(Math.Fit) And (angle.min - angle.max) Are From Another Script Which Are Imported By Adding "using SharedObjects;".
if(needle.localRotation != Quaternion.Euler(needle.localEulerAngles.x,needle.localEulerAngles.y,Math.Fit(value,maxValue,angle.min,angle.max)))
{
needle.localRotation = Quaternion.Euler(needle.localEulerAngles.x,needle.localEulerAngles.y,Math.Fit(value,maxValue,angle.min,angle.max));
}
}
Both Lines, In (If) Statement And Below Of The (If) Statement Send Errors!
Is There Anything I've Missed? Thanks..
Note: The Errors Are From Lines That Contain Quaternion.Euler And Not The TRANSFORM It Self! As At The Top There Is if(needle) Which Checks If The Transform Is Assigned!
Answer by andrew-lukasik · Jul 24, 2017 at 03:36 PM
localRotation being a property of an Object (class/reference type) requires that this object ("thisOne.localRotation") is not null - otherwise it will throw exception. Your transform must be null
Btw. Using
if( QuaternionA==QuaternionB )
is as bad idea as float==float (Quaternions are bunch of floats inside). You compare quaternions like this:
Quaternion quaternionA;
Quaternion quaternionB;
if( Quaternion.Angle( quaternionA , quaternionB )<10f )
{
//angle between quaternionA and quaternionB is less than 10 degrees
}
if( Quaternion.Dot( quaternionA , quaternionB )>0.9f )
{
//difference between quaternionA and quaternionB is less than 5%
//dot returns values ranging from -1f to 1f, where -1f is opposite direction and 1 for perfectly aligned (but again, these are floats, and you must test them using < and > and definately not ==)
}
You $$anonymous$$ean The (needle) Transform Has To Be [null]?? Or If Not Then How Do I Fix This Error? I Didn't Understand What You $$anonymous$$eant By Saying: "Your transform must be null", then what's the point of having a Transform to be null? :)
Yup. Your variable called "needle" must be null, you most probaly just forgot to assign any Transform to that field in the Inspector (check it out). I don not see any other reference type in your code here. Quaternions, Vector3, float etc are structs/value types so they will never throw NullReferenceException - it's reserved to variables/fields of reference types (anything class related).
Btw. I updated original post with proper ways to test Quaternions
The Variable Is Assigned In Inspector And Even I Click On It And It Shows $$anonymous$$e The Assigned Transform, But The Script Gives That Error Which Doesn't $$anonymous$$ake Any Sense, It's The Same As The JavaScript Version, But I Don't $$anonymous$$now Why It Is Happening :/
I think (Quaternion == Quaternion) isn't a bad idea in the method im using. its not Lerping the quaternion! it changes it instantly, and im checking if the Quaternion has changed then update the Quaternion.
And It Works Fine In JavaScript But Not In C#
$$anonymous$$y comment on "!=" operator between two Quaternions (or Vector3 etc. everything containing floats inside in general) is an euphemism of "it does not work! (as you think it does at least)". $$anonymous$$eaning: your comparison will probably always return true (!), because floats (hence Quaternions) are rarely equal when compared (even when equal in our opinion)