Search Unity

Checking for null

Discussion in 'Scripting' started by DarkNeo, Aug 9, 2014.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Hi guys

    So I am getting the following error below.


    MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it.Your script should either check if it is null or you should not destroy the object.

    The error message is pointing the below code..

    Code (CSharp):
    1. void updateCameraPosition()
    2.     {
    3.         if( !_playerController )
    4.         {
    5.  
    6.             transform.position = Vector3.SmoothDamp( transform.position, target.position - cameraOffset, ref _smoothDampVelocity, smoothDampTime );
    7.             return;
    8.        
    9.  
    10.         }
    On this exact line

    Code (CSharp):
    1. transform.position = Vector3.SmoothDamp( transform.position, target.position - cameraOffset, ref _smoothDampVelocity, smoothDampTime );
    So basically my gameObject gets destroyed and this part of the code is still accessing it. I would like to be checking for null I have tried the following below with no luck.

    Code (CSharp):
    1. if( !_playerController )
    Code (CSharp):
    1. if( _playerController == null)
    Code (CSharp):
    1. if( _playerController != null)
    Does someone have a better way around this? or to stop it from accessing my gameObject?

    Thank you
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    As a MonoBehaviour gets destroyed, OnDisable and OnDestroy are called. You need to make sure that at least after OnDestroy was called, you are not anymore calling updateCameraPosition.
     
    DarkNeo likes this.
  3. DiRe

    DiRe

    Joined:
    Jun 14, 2013
    Posts:
    12
    Hi,
    Are you sure that you used Destroy? not DestroyImmediately?
     
  4. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    That's my death script I am using, yeah I just use Destory.

    Code (JavaScript):
    1. var isDead = false;
    2. var death : AudioClip;
    3. function OnGUI()
    4. {
    5.     if (isDead)
    6.     {
    7.         GUI.Box(Rect(630, 100, 270, 250), "You're dead");
    8.         if(GUI.Button(Rect(640,150,250,100), "Try Again"))
    9.         {
    10.             Application.LoadLevel("JacksJacketendless");
    11.             isDead = false;
    12.            
    13.         }
    14.     }
    15. }
    16. function OnCollisionEnter2D(jack : Collision2D)
    17. {
    18.     if (gameObject != null )
    19.     {  
    20.         Destroy(jack.gameObject);
    21.     }
    22.     isDead = true;
    23. }
     
  5. DiRe

    DiRe

    Joined:
    Jun 14, 2013
    Posts:
    12
    Ok. what does it , target.position? in your scripy maybe that is null and you have to check if it is not null?
     
  6. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    It's targeting my character, the script makes his gameObject get destroyed.

    but the code Is still updating. not sure how to check if it is not null, :S

    Code (CSharp):
    1. void updateCameraPosition()
    2.     {
    3.         if( !_playerController )
    4.         {
    5.             transform.position = Vector3.SmoothDamp( transform.position, target.position - cameraOffset, ref _smoothDampVelocity, smoothDampTime );
    6.             return;
     
  7. DiRe

    DiRe

    Joined:
    Jun 14, 2013
    Posts:
    12
    just check if(target != null)
     
    DarkNeo likes this.
  8. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    Awesome, that is the one thank you very much DiRe! You are a champ!

    Cheers mate :D
     
unityunity