- Home /
[2D] [C#] Sprite disappears after changing position
I've run into a problem that's been bothering me a little bit. I'm sure the cause of it is something very simple, but I can't seem to think of anything.
My scene currently has a set of spikes and a player. Whenever the player collides with the spikes, it's supposed to respawn at a checkpoint. I've used the following scripts for this:
KillPlayer (attached to Spikes)
public LevelManager levelManager;
void Start ()
{
levelManager = FindObjectOfType<LevelManager>();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "player")
{
levelManager.RespawnPlayer();
}
}
LevelManager (attached to empty gameobject in scene)
public GameObject currentCheckpoint;
private PlayerController player;
void Start ()
{
player = FindObjectOfType<PlayerController>();
}
public void RespawnPlayer()
{
Debug.Log("Player respawned.");
player.transform.position = currentCheckpoint.transform.position;
}
I know this script works, because when I move the player into the spikes, the camera moves back to the position of the checkpoint. However, after this happens, I can't see my player anymore.
After some research I found that the problem might have something to do with the Sprite Renderer's Sorting Layer being set on default, but I'm too much of a newbie to know what I'm supposed to change to fix this.
To be sure, here's a few screenshots.
Probably is a simple issue with axis or rotation when you respawn the player. I didn't understand you question exactly, but for sure you need do keep your player "alive", just enable and disable, changing the position to the previous checkpoint. $$anonymous$$aybe you can create a Singleton in this case if you are changing the scene too.
Check the 'Z' of the transform o your camera and 'disappearing_gameObject' before and after doing the your update in game. The case should be that the z value of either of those gameobjects is changing.
Yep, looks like it was the Z-axis being wrong after all. Thank you very much for your answers!
Answer by komodor · Jun 18, 2015 at 10:09 AM
player.transform.position = new Vector3(currentCheckpoint.transform.position.x, currentCheckpoint.transform.position.y, player.transform.position.z);