Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

NullReferanceException catch isnt working

Discussion in 'Scripting' started by tomahawkit, Jun 21, 2016.

  1. tomahawkit

    tomahawkit

    Joined:
    May 17, 2016
    Posts:
    3
    so originally this script worked, but then i wanted to show by project to a friend so i packed it in zip. so when i closed my project and opened it it gave me this error. so i tried different catches to get rid of it and none of them work. so i was wondering two things, why did reloading the project give me the error, and what is the proper way to catch this exception? basically i know that script.carriedObject will be null untill the player picks up the object. and the code will only run if the carried object "this" carried object. so originally it was the third catch. but then i tried the other two because i thought that the third was improper. i tried looking through the forums and i found nothing, this is also my first time posting so if anyone has any advice on the post itself please give.

    Code (csharp):
    1.  
    2.     void OnCollisionEnter(Collision collision)
    3.     {
    4.         if (script.carriedObject)
    5.         {
    6.             print(script.carriedObject);
    7.         }
    8.         else
    9.         {
    10.             print("none");
    11.         }
    12.         /*
    13.         if (script.carriedObject != null)
    14.         {
    15.             if (script.carriedObject == this.gameObject)
    16.             {
    17.                 //script.normal = collision.contacts[0].normal;
    18.                 script.carriedObject.GetComponent<Rigidbody>().freezeRotation = false;
    19.                 script.colide = true;
    20.             }
    21.         }
    22.         */
    23.     }
    24.  
     

    Attached Files:

    Last edited: Jun 21, 2016
  2. jimroberts

    jimroberts

    Joined:
    Sep 4, 2014
    Posts:
    560
  3. tomahawkit

    tomahawkit

    Joined:
    May 17, 2016
    Posts:
    3
  4. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    You need to be more specific. What do you mean by "this error"? And at what line was it being thrown according to the error logs in the console? NullReferenceExceptions are hardly unique. In fact, questions about them are among the most common on these forums.

    The proper way is to never catch that exception at all, but rather fix the error that is causing the exception.

    In general, exceptions should not be caught unless you plan on doing something useful with them. Suppressing the exceptions is not useful at all.

    Anyways, looking at your code, I would bet that the member variable named "script" is probably null. You might want to double-check that and sort it out if that is indeed the case.
     
    LeftyRighty likes this.
  5. tomahawkit

    tomahawkit

    Joined:
    May 17, 2016
    Posts:
    3
    thank you so much for teaching me about the forms. and thanks for the advice, the problem was that script = GameObject.FindGameObjectWithTag("Player").GetComponent<PickupObject>(); wasnt actually finding the script. its fixed now, thank you
     
  6. shaderop

    shaderop

    Joined:
    Nov 24, 2010
    Posts:
    942
    My pleasure. And sorry if I sounded preachy. Wasn't my intention at all.

    Good luck!
     
unityunity