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 Anthony0506 · Nov 04, 2018 at 08:05 PM · exceptions

Does using try and catch actually fix the problem?

Hi. I'm getting a Null Exception error. I know why it's happening, I think. but that's not my question.

so I was sick of seeing the error, so I applied a Try and Catch. like this.

     void DetectMoveableObject(){
 
         if (moveableObjectFound == false && Input.GetButton ("Square")) {
             try {
                 moveableObject = PushPullObjectDetection ();
             }       
             catch (NullReferenceException ex) {
                 moveableObject = null;
             }

this stops the error from getting thrown in the console, and the game runs fine in the editor during test play. does this mean the problem isn't a problem anymore? i know the error doesn't get thrown, but is the problem not a problem anymore? does catch actually fix the problem, or just make the error message go away? will it stop a build from compiling?

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
1
Best Answer

Answer by IvovdMarel · Nov 04, 2018 at 08:16 PM

The error will no longer pop up, and your code will compile, but it definitely does not fix the problem. You're basically telling the program to 'try' and run this code, but if there is any error you will 'catch' it yourself. Generally in the catch statement you would put a Debug.LogError or Debug.LogWarning to ensure you're aware this situation is still happening.

If you get a null reference you have to find out why it is null - in this case, it must be somewhere in your PushPullObjectDetection function. If you don't do this, later on, unexpected things will happen (the game might not work as expected, you get no errors, it's almost impossible to find out what's causing it).

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 Anthony0506 · Nov 04, 2018 at 11:12 PM 0
Share

well the thing is, i expect it to come back null. the script runs like if you press square, you search for an object. if an object is found you grab it, it returns null if nothing is found. the error only occurs when you find the object, and then drop it immediately by mashing the input button. the script chains into multiple different methods.

when you say unexpected things will happen, do you mean when the script interacts with other scripts in the future? or do you mean that it will react differently than it is right now even if there was nothing new built off or around the script as it is now?

full code is as follows, maybe i'm just missing something glaringly obvious?

 public GameObject PushPullObjectDetection ()
     {
 
         Collider[] moveableObjectColliders = Physics.OverlapSphere (this.transform.position, moveableObjectDetectArea);
         float $$anonymous$$Distance = $$anonymous$$athf.Infinity;
         Collider moveableObject = null;
 
         foreach (Collider box in moveableObjectColliders) {
             if (box.gameObject.tag == "$$anonymous$$oveable Object") {
                 moveableObjects.Add (box.gameObject);
                 float distance = Vector3.Distance (box.transform.position, this.transform.position);
                 if (distance < $$anonymous$$Distance) {
                     moveableObject = box;
                     $$anonymous$$Distance = distance;
                 } else if (distance > $$anonymous$$Distance) {
                     moveableObject = null;
                     $$anonymous$$Distance = distance;
                 } else if (distance == $$anonymous$$Distance) {
                     moveableObject = null;
                     $$anonymous$$Distance = distance;
                 }
 
             }
         }
 
         if (moveableObjects.Count != 0) {
             moveableObjectFound = true;
 
         }
 
 
         if (moveableObjectFound == false) {return null;}
         if (moveableObjectFound == true ) {return moveableObject.gameObject;}
         return null;
 
     }



EDIT: I think I figured it out. i just had to clear the array before returning the gameObject. I know that I could have avoided using arrays completely, being that it's only a single gameObject, but I'd already been using the script and it was just a quick copy paste thing.

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

94 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

Related Questions

"Missing Collider2D" 2 Answers

How can i fix error CS1056 & CS1003 in Unity AR Multiuser Application? 0 Answers

I am not getting Debug.LogError or exceptions messages in Android Development Build 0 Answers

Error building Player: Exception: apk 0 Answers

UnassignedReferenceException 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