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 Kek_Kek · May 27, 2019 at 10:50 PM · nullreferenceexceptionnullif statement

Problem with checking if null

I am making a small riddle game and you are given a random riddle. When you solve the riddle, the riddle game object is destroyed. I made an array with all the riddles and the script randomly picks 1 riddle. The problem is that if(currentRiddle == null) doesn't work and it moves to if(currentRiddle != null) which works and it causes an error. Is there any way to fix this?

 public void PickRandomRiddle()
 {
     
     index = Random.Range(0, Riddles.Length);      
     currentRiddle = Riddles[index];
     

     if (currentRiddle == null)
     {
         PickRandomRiddle();
     }
     else if(currentRiddle != null)
     {
         currentRiddle.SetActive(true);
         print(currentRiddle.name);
     }
     
    
 }

Comment
Add comment · Show 2
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 Bunny83 · May 28, 2019 at 12:05 AM 0
Share

How is "Riddles" defined? what's it's type?

Also

and it causes an error

is completely useless. What error? C# and Unity for the most part actually has human readable errors which are accompanied with a stacktrace which tells you exactly where the error occured.


Note that your approach is really dangerous. You call your method recursively until you find by chance a non-null object. In the case that all objects have been destroyed you will run into a stack overflow exception since you can never exit that method and the method keeps calling itself until you run out of stack memory. Even when you ensure that there's at least one non null element in the array, depending on the element count it could take a lot recursion steps. If the element count is high enough (and you are unlucky enough) you can also run into a stack overflow.

avatar image Kek_Kek Bunny83 · May 28, 2019 at 12:16 AM 0
Share

Riddle is a public GameObject array public GameObject[] Riddles

I get a null reference exception error on line 14.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by cs120319992 · May 28, 2019 at 02:01 PM

Because currentRiddle have a reference 'Riddles[index]' and it always will be not null. And error?

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

110 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

Related Questions

Checking if an object is null, results in NRE 1 Answer

Testing if object is null creates a NullReferenceException. 1 Answer

Missing Reference after null Check 4 Answers

Assign collider and linerenderer to list 1 Answer

reference an object that is yet to be in the scene 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