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 /
  • Help Room /
avatar image
-2
Question by dimitrakis1992 · Jan 12, 2017 at 01:31 PM · c#camera

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");
     }
 }


Comment
Add comment · Show 3
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 tanoshimi · Jan 12, 2017 at 01:33 PM 0
Share

That's your only script? It suggests that something is destroying your camera's target object.

avatar image Pengocat · Jan 12, 2017 at 01:41 PM 0
Share

Also there is no need to use FixedUpdate() for anything that is not related to physics. You should simply use Update() or LateUpdate().

avatar image Hellium · Jan 12, 2017 at 01:53 PM 0
Share

@dimitrakis1992 : Question edited so as to add the two additional scripts you provided

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · 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
0

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.

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 dimitrakis1992 · Jan 12, 2017 at 02:38 PM 0
Share

ok thank you sir.I fix it

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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