- Home /
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.
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.
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?
Just found the issue.
Change:
cf.player = gameObject;
To:
cf.player = P;
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.
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.