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 /
avatar image
0
Question by Tekksin · Oct 22, 2013 at 10:50 AM · camera2d2d-platformerrespawndeath

How do I get my 2D game camera to view the respawned sprite?

I'm brand new to Unity, and I'm having a bit of trouble. I'm creating a 2D platformer very much like Mario. When the player falls down a hole, another is created at the spawn point, but the camera doesn't look at it.

I have a script for my camera to follow my player, but when my player dies, I not only get the error:

MissingReferenceException: The object of type 'GameObject' 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.Update () (at Assets/Scripts/CameraFollow.js:5)

But my camera also doesn't relocate to the spawn point and look at my instantiated character. I'm not sure what I have to do to code this properly.

The code on my camera is very simple [title: CameraFollow]:

     var player : GameObject;
      
     function Update () {
      
     this.gameObject.transform.position = new Vector3(player.gameObject.transform.position.x, this.gameObject.transform.position.y, this.gameObject.transform.position.z);
 }

And the code on my Player Respawn script is relatively simple [title: PlayerRespawn]:

 #pragma strict
 
 var Player : GameObject;
 var spawnPoint : Transform;
 
 function OnTriggerEnter(other : Collider) {
         Destroy(other.gameObject);
         var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
         var cf = Camera.main.GetComponent(CameraFollow);
         var instance : GameObject;
         
 }

Can anybody explain to me, in a way that allows a n00b to comprehend it, how to get unity to stop telling me that my gameobject is destroyed and needs to either be null/not destroyed (basically my error code), and especially how to relocate the camera to the new player which IS being created--it's just not being looked at by my Main Camera.

Thanks for the help, and I'm sorry if I didn't explain things thoroughly enough.

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

1 Reply

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

Answer by dorpeleg · Oct 22, 2013 at 10:52 AM

Just add:

 cf.player = gameObject;

After:

 var cf = Camera.main.GetComponent(CameraFollow);

Basically what happens is, You destroy the gameObject (player) when you fall into a hole.

Now, the camera is trying to follow a destroyed gameObject.

That is why you are getting the error.

You can fix that in two ways, either by telling the camera to follow the new player you are creating at spwan (like my code above) or, by not destroying the player and only moving it back to spwan point.

Comment
Add comment · Show 18 · 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 Tekksin · Oct 22, 2013 at 11:52 AM 0
Share

Dorpeleg, you're awesome. Thank you.

That got rid of the error for me. I've progressed a little in the project, and am noticing a problem I can't shake.

In the initial post I mentioned that the camera doesn't follow the player after the player dies. What's interesting is that I created an enemy, and when the player gets hit by the enemy and dies, the camera starts to track the enemy! I mean...how?? I made the enemy AFTER I made the camera script. So strange.

$$anonymous$$y camera script is the same,

     var player : GameObject;
      
     function Update () {
      
     this.gameObject.transform.position = new Vector3(player.gameObject.transform.position.x, this.gameObject.transform.position.y, this.gameObject.transform.position.z);
 }


but my PlayerRespawn script reads:

 #pragma strict
 
 var Player : GameObject;
 var spawnPoint : Transform;
 
 function OnTriggerEnter(other : Collider) {
     if(other.tag == "Player"){
         Destroy(other.gameObject);
         var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
         var cf = Camera.main.GetComponent(CameraFollow);
         cf.player = gameObject;
     }
 }

So if somebody can help me figure out why the camera follows the enemy after the Player dies, that would be awesome! Thanks for your help.

EDIT: VERY I$$anonymous$$PORTANT! The camera follows the enemy only if the enemy kills the player. If the player falls down the hole into the death zone, then the camera doesn't do anything. Solutions?

avatar image dorpeleg · Oct 22, 2013 at 12:31 PM 0
Share

Just found the issue.

Change:

 cf.player = gameObject;

To:

 cf.player = P;
avatar image Tekksin · Oct 22, 2013 at 12:32 PM 0
Share

Nope. If I fall down the hole it goes back to the beginning of the level, but doesn't follow the player, and if I'm killed by the enemy, it follows the enemy lol.

avatar image dorpeleg · Oct 22, 2013 at 12:37 PM 0
Share

just edited my comment, please look.

avatar image Tekksin · Oct 22, 2013 at 01:03 PM 0
Share

PERFECT! well almost perfect lol. The camera does indeed focus on the player when he falls down a hole, but if an Enemy kills him, it stats to track/follow the enemy.

You're helping me so much, I hope I don't get to a point where you have no idea what's going on. Thanks a lot for your help, man.

Show more comments

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

15 People are following this question.

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

Related Questions

Mario like 2D Camera 2 Answers

Help with a 2d camera Controller 1 Answer

Advice on implementing metroidvania camera 1 Answer

How do I kill off my player, reduce his lives and respawn him? 3 Answers

2D Platformer - Problem with object that kills, then restarts after build 1 Answer


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