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 /
avatar image
0
Question by maltakereuz · Feb 27, 2018 at 12:31 PM · nullunassignedreferenceexcept

UnassignedReferenceException and Null-Check

I often check in Start()-method, if all needed references are set in inspector. So i wrote a utility method, for this like this:

 public static void WarnIfNull(object o, string name) {
     if (o == null) {
         Debug.LogWarning(name + " is NULL");
 ...


so i can use it in component init code like this:

 public class MyComponent : MonoBehavior {
     public AudioClip sound;
     void Start() {
         MyLogUtils.WarnIfNull(sound, "sound");
 ...

Nothing special, interesting so far... But it does not work. Assuming i forgot to assign reference in inspector. If i null-check inside of MyComponent.Start(), null check works correct. But if doing exact same null-check inside of MyLogUtils.WarnIfNull(), null check is FALSE. I can not explain this, it is really weird.

So i tryied to debug it, if object is not null, then what is it? (it was never assigned). I can cast it to AudioClip like this

 if (o is AudioClip) {
     AudioClip a = (AudioClip) o; // this works
 }

but if i call any method/property from this object like this:

 var a_name = a.name;
 a_name.ToString();

i am getting UnassignedReferenceException.

I know, it sounds strange, that null-check of same object in call-stack behaves not same. But is looks like this. I do not really understand what is the reason for that. Docs for UnassignedReferenceException somehow do not explain it.

So my missing-reference method looks like this now:

 public static void WarnIfNull(object o, string name) {
     bool unassigned = false;
     if (o is AudioClip) {
         try {
             AudioClip a = (AudioClip) o;
             var a_name = a.name;
             a_name.ToString();
         } catch (UnassignedReferenceException e) {
             unassigned = true;
         }
     }
     if (o == null || unassigned) {
         Warn(name + " is NULL (unassigned in inspector, but somehow can be not null)");
     }
 }


Any suggestions explanations are welcome. So, my question:

Why the same reference is null in one methode and not null in another. I just pass it by reference but, null-check result ist different. Is this some special UnityEditor stuff i should know?

Comment
Add comment · Show 1
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 Hellium · Feb 27, 2018 at 01:49 PM 0
Share

Have you tried changing the signature of your utility function to:

 public static void WarnIfNull(UnityEngine.Object o, string name)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Vencarii · Feb 27, 2018 at 01:45 PM

So you don't get any error when you use MyLogUtils.WarnIfNull(sound, "sound"); in Start()?

My guess would be that MyLogUtils isn't there in your Start() method, maybe?

Comment
Add comment · Show 1 · 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 maltakereuz · Feb 27, 2018 at 04:17 PM 0
Share

So you don't get any error when

Exactly. I just forgot to set sound to one component, and wonder that no warning was on console. I suspect that UnityEditor makes some art of fake-dummy-object for some reason. So I need some reliable way to detect missing referneces. Just null check seems not to be enought.
avatar image
0

Answer by maltakereuz · Feb 28, 2018 at 04:52 AM

found answer: https://blogs.unity3d.com/2014/05/16/custom-operator-should-we-keep-it/

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

76 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

Related Questions

Testing if object is null creates a NullReferenceException. 1 Answer

[UNSOLVED] Object reference not set to an instance of an object 0 Answers

Inspector And Variable Inconvenience? 0 Answers

Check if GameObject.Find returns null 1 Answer

Unity GetComponent return value 2 Answers


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