Please help me with this error
Hello to everyone.My goal is to build a 2D platform game.I have this code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Camerafollow2DPlatformer : MonoBehaviour {
 
     public Transform target;
     public float smoothing;
 
     Vector3 offset;
     float lowY;
 
     // Use this for initialization
     void Start () {
         offset = transform.position - target.position;
         lowY = transform.position.y;
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         Vector3 targetCamPos = target.position + offset;
         transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
         if (transform.position.y < lowY) transform.position = new Vector3 (transform.position.x, lowY, transform.position.z);
     }
 }
 
 
               I am getting this error: 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. UnityEngine.Transform.get_position () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/TransformBindings.gen.cs:26) Camerafollow2DPlatformer.FixedUpdate () (at Assets/Scripts/Camerafollow2DPlatformer.cs:21)
The problem is in line 21 but i can't fix it.Please help me.
No my playerScript
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerController : MonoBehaviour {
 
     public float maxSpeed;
     public Vector3 respawnPoint;
 
     bool grounded = false;
     float groundCheckRadius = 0.2f;
     public LayerMask groundLayer;
     public Transform groundCheck;
     public float jumpHeight;
 
     Rigidbody2D myRB;
     Animator myAnim;
     bool facingRight;
 
     public Transform gunTip;
     public GameObject bullet;
     float fireRate = 0.5f;
     float nextFire = 0f;
 
     // Use this for initialization
     void Start () {
         myRB = GetComponent<Rigidbody2D> ();
         myAnim = GetComponent<Animator> ();
         respawnPoint = transform.position;
 
         facingRight = true;
 
     }
     
     // Update is called once per frame
 
     void Update(){
         if (grounded && Input.GetAxis ("Jump") > 0) {
             grounded = false;
             myAnim.SetBool ("isGrounded", grounded);
             myRB.AddForce (new Vector2 (0, jumpHeight));
         }
 
         if (Input.GetAxisRaw ("Fire1") > 0) fireRocket ();
 
     }
 
 
 
     void FixedUpdate () {
 
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, groundLayer);
         myAnim.SetBool ("isGrounded", grounded);
         myAnim.SetFloat ("VerticalSpeed", myRB.velocity.y);
 
         float move = Input.GetAxis ("Horizontal");
         myAnim.SetFloat ("speed", Mathf.Abs (move));
         myRB.velocity = new Vector2 (move * maxSpeed, myRB.velocity.y);
 
         if (move > 0 && !facingRight) {
             flip ();
         } else if (move < 0 &&facingRight) {
             flip ();
      }
 }
     void flip(){
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *=-1;
         transform.localScale = theScale;  
     }
 
     void fireRocket(){
         if (Time.time > nextFire) {
             nextFire = Time.time + fireRate;
             if (facingRight) {
                 Instantiate (bullet, gunTip.position, Quaternion.Euler (new Vector3 (0, 0, 0)));
             } else if (!facingRight) {
                 Instantiate (bullet, gunTip.position, Quaternion.Euler (new Vector3 (0, 0, 180f)));
             }
         }
     }
 
     void OnTriggerEnter2D(Collider2D other){
         if (other.tag == "cleaner") {
             transform.position = respawnPoint;
         }
         if (other.tag == "Checkpoint") {
             respawnPoint = other.transform.position;
         }
     }        
 }
 
 
               My playerHealth Script
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerHealth : MonoBehaviour {
 
     public AudioClip PlayerDeathSound;
     public float fullHealth;
     public GameObject deathFX;
     public AudioClip playerHurt;
 
     public restartGame theGameManager;
 
     float currentHealth;
     PlayerController controlMovement;
 
     AudioSource playerAS;
 
     public Slider healthSlider;
     public Image damageScreen;
     public Text gameOverScreen;
     public Text winGameScreen;
 
     bool damaged = false;
     Color damagedColour = new Color(0f,0f,0f,0.5f);
     float smoothColour = 5f;
 
 
     // Use this for initialization
     void Start () {
         currentHealth = fullHealth;
         controlMovement = GetComponent<PlayerController> ();
 
         healthSlider.maxValue = fullHealth;
         healthSlider.value = fullHealth;
 
         damaged = false;
 
         playerAS = GetComponent<AudioSource> ();
     }
     
     // Update is called once per frame
     void Update () {
         
         if (damaged) {
             damageScreen.color = damagedColour;
         } else {
             damageScreen.color = Color.Lerp (damageScreen.color, Color.clear, smoothColour * Time.deltaTime);
         }
         damaged = false;
    }
 
     public void addDamage(float damage){
         if (damage <= 0) return;
         currentHealth -= damage;
         playerAS.clip = playerHurt;
         playerAS.Play ();
         //playerAS.PlayOneShot (playerHurt);
 
 
         healthSlider.value = currentHealth;
         damaged = true;
 
         if (currentHealth<=0){
             makeDead ();
      }
 }
 
     public void addHealth(float healthAmount){
         currentHealth += healthAmount;
         if (currentHealth > fullHealth) currentHealth=fullHealth;
         healthSlider.value = currentHealth;
     }
 
     public void makeDead (){
     Instantiate(deathFX, transform.position, transform.rotation);
     Destroy(gameObject);
     AudioSource.PlayClipAtPoint (PlayerDeathSound, transform.position);
     damageScreen.color = damagedColour;
 
         Animator gameOverAnimator = gameOverScreen.GetComponent<Animator> ();
         gameOverAnimator.SetTrigger ("gameOver");
   }
 
     public void winGame(){
         Destroy (gameObject);
         //theGameManager.restartTheGame ();
         //Animator winGameAnimator = winGameScreen.GetComponent<Animator> ();
         //winGameAnimator.SetTrigger ("gameOver");
     }
 }
 
              That's your only script? It suggests that something is destroying your camera's target object.
Also there is no need to use FixedUpdate() for anything that is not related to physics. You should simply use Update() or LateUpdate(). 
@dimitrakis1992 : Question edited so as to add the two additional scripts you provided
Answer by LK84 · Jan 12, 2017 at 01:42 PM
Somewhere else in your program the GameObject with the Transform Component target gets destroyed but you still are trying to acces it. If you have a constant, not moving target position you can instead just set the target Position as a Vector3 in the Start() method and then change line 21 to:
 Vector3 targetCamPos = targetPosition + offset; //targetPosition is a Vector3 set in the Start() method
 
               EDIT (with your new provided script);
I assume you drag a GameObject containing the playerhealth script on the public Transform targetfield? Since the gameobject gets destroyed in line 78 of PlayerHealth you can't reference it any more from your Camera Follow script
Answer by gcoope · Jan 12, 2017 at 01:44 PM
Above line 21 add a new line:
 if(target == null) return;
 
               This will stop any code executing after it if the target has been destroyed - which your error log suggests it has. I would also look at why your target transform object is getting destroyed.
Your answer
 
             Follow this Question
Related Questions
Camerashake doesn´t work expected in Build c# 0 Answers
How do I offset a camera when it's position is defined by a VR controller 0 Answers
Change Camera postion 1 Answer
How to change camera on Android? 1 Answer
2D Camera boundaries C# 2 Answers