Search Unity

I can't get the the null check to stop my camera from following the player after they die

Discussion in 'Scripting' started by PopRockLex, Dec 27, 2018.

  1. PopRockLex

    PopRockLex

    Joined:
    Sep 13, 2018
    Posts:
    17
    Hey all, I have a quick question I can't seem to find the answer to.
    I have my camera following the player object and when they die I get this

    "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.
    CameraFollow.FixedUpdate () (at Assets/Scripts/CameraFollow.cs:13)"

    This is my current script
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CameraFollow : MonoBehaviour
    4. {
    5.  
    6.     public Transform player;
    7.     public Vector3 offset;
    8.  
    9.     void FixedUpdate()
    10.     {
    11.  
    12.         Vector3 setPosition = transform.position;
    13.         setPosition.y = player.transform.position.y + offset.y;
    14.         setPosition.z = player.transform.position.z + offset.z;
    15.         transform.position = setPosition;
    16.     }
    17.  
    18. }
    I have another game object following the player as well and use "if (player != null)" before the main script on that to stop the script if the player is dead however when I add it in fixed update above the Vector3 setPosition I get
    "Error CS1023 Embedded statement cannot be a declaration or labeled statement" for line 13 which is the Vector3 line, and 3 more error messeges telling me setPosition does not exist in current context for the following 3 lines. I'm not sure what I'm doing wrong, any help would be appreciated.
    Thanks!
     
    Last edited: Dec 27, 2018
  2. First of all, please, put your code in code tags. How to is here.

    Second, please post what kind of error messages you get and what is the code you're trying.

    Alternatively you can try to disable the CameraFollow script when your player dies and turn back on when you respawn it.
     
  3. PopRockLex

    PopRockLex

    Joined:
    Sep 13, 2018
    Posts:
    17
    My bad! I fixed the code tag and posted the error messages.
     
  4. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    My guess is you forgot the brackets.

    Code (CSharp):
    1. using UnityEngine;
    2. public class CameraFollow : MonoBehaviour
    3. {
    4.     public Transform player;
    5.     public Vector3 offset;
    6.     void FixedUpdate()
    7.     {
    8.         if( player != null ) {
    9.             Vector3 setPosition = transform.position;
    10.             setPosition.y = player.transform.position.y + offset.y;
    11.             setPosition.z = player.transform.position.z + offset.z;
    12.             transform.position = setPosition;
    13.         }
    14.     }
    15. }
     
    PopRockLex and Lurking-Ninja like this.
  5. Agree.
     
    PopRockLex likes this.
  6. PopRockLex

    PopRockLex

    Joined:
    Sep 13, 2018
    Posts:
    17
    You are quite right! Well, I feel silly. Thank you for your help!