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
1
Question by Extrumus · Sep 07, 2018 at 04:24 PM · errorbugs

Why does empty if statement fix null reference

Hello, today I was trying to centralize my code and I have run into an interesting problem with even more fascinating solution. So the script which is a grandchild of the script that it is finding returns error:

         NullReferenceException: Object reference not set to an instance of an object

is while trying to debug it I wrote an if statement that checks if the object is not null at the start and the problem was fixed . Here is how the code looks like after deleting the Debug.Log()

     private PlayerControler pControlerSelect;

 void Start () {
     pControlerSelect = FindObjectOfType<PlayerControler>();

     if (pControlerSelect != null)
     {
     }

     WeaponSelect();
 }

also the error fires from the line that is in method WeaponSelect() and changing the statement to check if the object is null returns the error

Comment
Add comment · Show 7
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 Nikola7007 · Sep 07, 2018 at 04:33 PM 0
Share

That is weird! Can you post the actual line in WeaponSelect() that throws the error?

avatar image Extrumus Nikola7007 · Sep 07, 2018 at 04:35 PM 0
Share
 private void WeaponSelect()
     {
         int index = 0;
         foreach (Transform weapon in transform)
         {
             if (index == selectedWeapon)
             {
                 weapon.gameObject.SetActive(true);
             }
 
             else
             {
                 weapon.gameObject.SetActive(false);
             }
             
             index++;
         }
 //the fallowing line
         pControlerSelect.UpdateCurrentWeapon();
 
     }
avatar image Nikola7007 Extrumus · Sep 07, 2018 at 04:48 PM 0
Share

Huh. I got nothing... that is super strange though. Hope you're able to figure it out

Show more comments
avatar image fct509 · Dec 08, 2019 at 04:05 AM 0
Share

Does your working if-statement have curly braces?

What I mean, does your code look like:

 // Option 1:
 if (pControlerSelect != null)
 {
 }
 pControlerSelect.UpdateCurrentWeapon();
 
 
 // Option 2:
 if (pControlerSelect != null)
 
 pControlerSelect.UpdateCurrentWeapon();


The thing is, if it looks like Option 2, then that's not an empty if-statement. If Option 2 is your situation, then the if-statement is stopping "pControlerSelect.UpdateCurrentWeapon();" from being executed when pControlerSelect is null.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Sep 07, 2018 at 06:41 PM

No this can't solve or influence anything. I suspect that you have a wrong if statement when you actually have a null reference exception. You most likely did

 if (pControlerSelect = null)

instead of

 if (pControlerSelect == null)

The first statement will actually set "pControlerSelect" to null while the second will check if it's null.

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 EmulatorsGaming · Dec 08, 2019 at 02:55 AM -1
Share

@Bunny83 The proper syntax for an if statement is

 if (theVaribleYouWant == thevalueyouwant)
 {
 then do this
 }

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

123 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 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

Error "OnTriggerEnter: this message has to be of type: Collider" 0 Answers

erros and bugs 1 Answer

Exporting to iOS device breaks game and causes errors 0 Answers

Strange bug after update to 5.6.0f1 (This is not supported) 2 Answers

Some functions do not work when I build the game.,Some functions don't work when I build the game 0 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