I made a 2d platformer game where there are traps for example disappearing floor. Once I die and respawn as a clone the scene doesnt get reseted.. If I try Application.Loadlevel(); it restarts everything and I can't add check points... This is the code. Also while standing on a slope makes my character slide back down it doesnt just stand there Code (CSharp): void OnGUI (){ if (text){ GUI.Label ( new Rect(Screen.width / 2.9f, Screen.height / 8, 444,138), YouDied); GUI.backgroundColor = Color.clear; bool menu= GUI.Button ( new Rect(Screen.width / 1.3f, Screen.height - (Screen.height * 0.9f / 4.3f), 185,67), MainMenu); bool retry= GUI.Button ( new Rect(Screen.width / 2.3f, Screen.height - (Screen.height * 0.9f / 4.3f), 185,67), pressSpacebar); GUI.Label ( new Rect(Screen.width / 2.13f, Screen.height - (Screen.height * 0.9f / 3.2f), 185,47), Retry); if (menu){ Application.LoadLevel(0); score = 0; } else if (Input.GetKey("space")){ text = false; var P = (GameObject)Instantiate(Player, spawnPoint.position, Quaternion.identity); P.name = "Player"; var sf = Camera.main.GetComponent<CameraFollow>(); sf.player = P.transform; Dea.enabled = false; ths.enabled = false; number.enabled = false; } } } please help
Not sure if I understand it correct, but why not showing the "final" screen in a different leve/scene with a button to restart the game (and reload the scene)?
If I restart (makes a clone instead of restarting the scene) it just makes a clone where my spawnpoint is at but I want the scene to reset when I respawn. Basically want to do to Application.Loadlevel but instead loads at all my spawnpoints
You could make a static vector3 variable and always assign the latest (re)spawn point to it. When you die, reload the level and make your character spawn at that position rather than a hardcoded starting position. As it's static, it should always keep the value until you quit the game (or change it, of course).
How would I do that? Code (CSharp): using UnityEngine; using System.Collections; public class GameOver : MonoBehaviour { public bool text = false; public Texture YouDied; public Texture MainMenu; public Texture pressSpacebar; public Texture Retry; public GameObject Player; public Transform spawnPoint; public bool text2 = false; public static int score = 0; public static int currentScore = 0; public GUIText Deaths; public GUIText TryAgain; public GUIText Menu; public GUIText PressSpacebar; public GUIText MenuHover; private CameraFollow cameraFollow; private Menu2 OnHover; void OnTriggerStay2D(Collider2D other){ if(other.tag == "Player"){ score += 1; Deaths.text = "<i><color=red>Dea</color><color=teal>ths:</color></i> " +score; Deaths.enabled = true; Menu.enabled = true; TryAgain.enabled = true; PressSpacebar.enabled = true; MenuHover.enabled = true; text2 = true; } if (gameObject.name == "Spikes"){ GetComponent<SpriteRenderer>().enabled = true; } text = true; Destroy(other.gameObject); } void OnGUI (){ if (text){ GUI.Label ( new Rect(Screen.width / 2.9f, Screen.height / 8, 444,138), YouDied); if (Input.GetKey("space")){ text = false; Application.LoadLevel(Application.loadedLevel); Deaths.enabled = false; Menu.enabled = false; TryAgain.enabled = false; PressSpacebar.enabled = false; MenuHover.enabled = false; } } } void Update (){ if(score < currentScore){ currentScore = score; } else{ currentScore = score; } } } This is my code when the player dies
Well, for me it wouldn't be that class to handle the spawning, but you got a spawnPoint of type Transform there. I'd say choose Vector3 for it and make it static, something like: Code (CSharp): public static Vector3 spawnPoint = new Vector3(yourX, yourY, yourZ); The assignment is just the initial/default value representing your very first spawnPoint, of course you have to pass valid arguments. Whenever you reach a checkpoint or a place to respawn in your game, you'll re-assign your variable with that position of the new spawnPoint. Your player should also be "spawned" via script in this case, no need to actually instantiate it with code, but at least set its position via script to GameOver.spawnPoint (note that GameOver.spawnPoint will only work if you make spawnPoint static).