Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 24, 2017 at 07:10 PM by $$anonymous$$ for the following reason:

Question is off-topic or not relevant

avatar image
1
Question by $$anonymous$$ · Jul 24, 2017 at 03:26 PM · transformquaternionnullreferenceexceptioneulerlocalrotation

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

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 ==)
 }

Comment
Add comment · Show 23 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Jul 24, 2017 at 03:40 PM 0
Share

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? :)

avatar image andrew-lukasik $$anonymous$$ · Jul 24, 2017 at 03:57 PM 0
Share

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

avatar image $$anonymous$$ andrew-lukasik · Jul 24, 2017 at 04:26 PM 0
Share

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

Show more comments
avatar image $$anonymous$$ · Jul 24, 2017 at 03:57 PM 0
Share

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#

avatar image andrew-lukasik $$anonymous$$ · Jul 24, 2017 at 04:12 PM 0
Share

$$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)

Follow this Question

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What does a Vector3 state? 1 Answer

How can I multiply the rotation of a mirrored object? 1 Answer

Problem with Quaternion.Euler() when rotating object 3 Answers

transform.localRotation resets after rotation 1 Answer

How do you set a local rotation for 1 axis but keep the other axis world space untouched?,How do you set the local rotation of 1 axis without effecting the world rotation of others? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges