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 LandonC · Feb 23, 2017 at 10:03 AM · nullreferenceexceptionif-elsenull referencestatic-vars

Checking nulls almost all the time! Is this normal?

The script I wrote below checks the state of a static variable "selectedObject" from another class, every time I want to access or change things inside this static variable, I have to check the if it is null to make sure the null error does not pop out.

I have written most of my scripts in this method and thought maybe there could be a better design for this. Any pointers or advice is much appreciated!

 if(myScript.selectedObject != null && (myScript.instance.notDead || Interface.selectedObject.fainted))
 {
     myScript.selectedObject.CheckState();
 }
 if(powerScript != null && powerScript.addPower < 0f)
 {
     powerScript.addPower = 10f;
 }
 
 if(myScript.selectedObject != null && myScript.selectedObject.activates)
 {
     Destroy(myScript.selectedObject.activates.gameObject);
     myScript.selectedObject.activates = null;
 }
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

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Nischo · Feb 23, 2017 at 01:42 PM

In your case it could be easier to rewrite your code that you can write an early out using

 if(myScript.selectedObject == null)
     return;

ensuring that the followed code can safely work with myScript.

Comment
Add comment · Show 2 · 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 LandonC · Feb 27, 2017 at 01:38 AM 0
Share

Thanks for this useful info! I am still learning C# through coding in Unity actually, for what I know about "return" is that it is used in loops, can it be used in normal functions as well?

So as per your suggestion I'm assu$$anonymous$$g I would write my code like this...

 if(myScript.selectedObject == null)
     {
           return;
      }
      else
      {
           if(myScript.instance.notDead || Interface.selectedObject.fainted))
          {
              myScript.selectedObject.CheckState();
          }
          if(powerScript != null && powerScript.addPower < 0f)
          {
              powerScript.addPower = 10f;
          }
          if(myScript.selectedObject.activates)
          {
              Destroy(myScript.selectedObject.activates.gameObject);
              myScript.selectedObject.activates = null;
           }
     }
     
 
 
 
 
avatar image mmciver LandonC · Feb 27, 2017 at 01:53 AM 1
Share

$$anonymous$$ore like:

     void Your$$anonymous$$ethod() {
         if(myScript.selectedObject == null) {
             return;
         }
 
         if(myScript.instance.notDead || Interface.selectedObject.fainted) {
             myScript.selectedObject.CheckState();
         }
         if(powerScript != null && powerScript.addPower < 0f) {
             powerScript.addPower = 10f;
         }
         if(myScript.selectedObject.activates) {
             Destroy(myScript.selectedObject.activates.gameObject);
             myScript.selectedObject.activates = null;
         }
     }
 

The distinction is that you don't have an unnecessary "else" that contains the entire rest of the function.

the return keyword is used to return a value from the function. So just the return keyword ends the function and nothing further in it is processed.

An alternate way to do it is to put the null check in the calling function, like:

         void Calling$$anonymous$$ethod() {
             if(myScript.selectedObject != null) {
                 Your$$anonymous$$ethod();
             }
         }
     
         void Your$$anonymous$$ethod() {
             if(myScript.instance.notDead || Interface.selectedObject.fainted) {
                 myScript.selectedObject.CheckState();
             }
             if(powerScript != null && powerScript.addPower < 0f) {
                 powerScript.addPower = 10f;
             }
             if(myScript.selectedObject.activates) {
                 Destroy(myScript.selectedObject.activates.gameObject);
                 myScript.selectedObject.activates = null;
             }
         }
 



I prefer this way, as it makes the logic a bit clearer to read.

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

7 People are following this question.

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

Related Questions

I keep getting a null reference exception, can anyone help? 0 Answers

NullReferenceException Error 1 Answer

i have written this code to spawn a segments of enemies i don't get errors at all but it didn't work .. what should i do ? 2 Answers

NullReferenceException yet Debug.Log shows nothing is null? 1 Answer

Null Reference Exception, what is it and why do I get it? 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