Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Danao · Sep 18, 2014 at 02:38 PM · cameraerrortransformnull

Expand script to stop camera from following player

Hi, I'm having a problem figuring out what to do with my script that has the camera follow the player when the player dies. I'm getting the following 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:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UnityEngineTransform.cs:28) Camera2DFollow.Update () (at Assets/Sample Assets/2D/Scripts/Camera2DFollow.cs:28)

Which takes me to the following code snippet:

 void Update () {
         
         // only update lookahead pos if accelerating or changed direction
         float xMoveDelta = (target.position - lastTargetPosition).x;
 
         bool updateLookAheadTarget = Mathf.Abs(xMoveDelta) > lookAheadMoveThreshold;
 
         if (updateLookAheadTarget) {
             lookAheadPos = lookAheadFactor * Vector3.right * Mathf.Sign(xMoveDelta);
         } else {
             lookAheadPos = Vector3.MoveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);    
         }
         
         Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
         Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
 
         transform.position = newPos;
         
         lastTargetPosition = target.position;
 
     }
 }


Any suggestions would be greatly appreciated! Thanks!

Comment
Add comment · Show 1
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 Graham-Dunnett ♦♦ · Sep 18, 2014 at 02:39 PM 0
Share

What are the exact error messages?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by dmg0600 · Sep 20, 2014 at 09:29 AM

Your target has been destroyed and the script is trying to access it. That's why it is giving you the missing exception.

Do you destroy the player object (or whatever object the target is) when the player dies?

Comment
Add comment · Show 5 · 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 Danao · Sep 20, 2014 at 09:46 AM 0
Share

Thanks for the response dmg0600! What I have is the main camera with the player and camera target as children. When the player is destroyed (which is working O$$anonymous$$),the camera target should be destroyed with its parent (correct?). I've been trying, unsuccessfully, to destroy the script to stop the camera from trying to access the target. Could you tell me the best way to do that (or if that is what I should be doing)?

avatar image dmg0600 · Sep 20, 2014 at 10:38 AM 1
Share

To disable what you are doing in the Update the best way to do it is putting this line first inside the Update: if (target == null) return;

 void Update () {
  
     if (target == null)
         return;
 
     // only update lookahead pos if accelerating or changed direction
     float x$$anonymous$$oveDelta = (target.position - lastTargetPosition).x;
  
     bool updateLookAheadTarget = $$anonymous$$athf.Abs(x$$anonymous$$oveDelta) > lookAhead$$anonymous$$oveThreshold;
  
     if (updateLookAheadTarget) {
         lookAheadPos = lookAheadFactor * Vector3.right * $$anonymous$$athf.Sign(x$$anonymous$$oveDelta);
     } else {
         lookAheadPos = Vector3.$$anonymous$$oveTowards(lookAheadPos, Vector3.zero, Time.deltaTime * lookAheadReturnSpeed);  
     }
  
     Vector3 aheadTargetPos = target.position + lookAheadPos + Vector3.forward * offsetZ;
     Vector3 newPos = Vector3.SmoothDamp(transform.position, aheadTargetPos, ref currentVelocity, damping);
  
     transform.position = newPos;
  
     lastTargetPosition = target.position;
  
 }

This way you can still use the script as it won't use the Update if there is no target. If you respawn the player, the only thing you have to do for the script to work again is assign the camera target to its variable.

avatar image Danao · Sep 20, 2014 at 10:57 AM 0
Share

Thanks for the excellent advice! That works perfectly. $$anonymous$$ay karma come your way in spades!!!

avatar image dmg0600 · Sep 20, 2014 at 11:36 AM 0
Share

Glad to help! If you can, mark it as answered.

avatar image Danao · Sep 20, 2014 at 11:49 AM 0
Share

Ah, I see I can't yet vote up (need 15 karma), but I can accept answers! Accepted and appreciated :)

avatar image
0

Answer by SnotE101 · Sep 18, 2014 at 05:18 PM

The level you want to load is not in the build settings.

Go to Use File->Build Settings.. and click add current, or make sure all your levels are in loaded in the build settings.

Good luck!

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 Danao · Sep 19, 2014 at 01:13 PM 0
Share

Thanks Graham and SnotE101, but I just checked the build settings and things look good there. I've edited my first post to include the error message I'm getting (and the Game Over screen will now load after the game crashes/pauses when the error for the transform object is thrown).

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

When Is This Script Destroyed? 1 Answer

A GUI error in script??? HELP,Please!! 1 Answer

Object Reference Error On Camera Script 1 Answer

Simple Script getting Errors. 1 Answer

UnityScript error in transform.Position 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