Question by
VibrantGaming · Dec 01, 2015 at 09:58 AM ·
variableslevelresetspawner
Resetting the level without changing variables
Hallo. I want to reset my level like a respawn type of thing. But I have variables like a score system in other scripts. Like I have a score system that everytime you reset the level you lose a score. But everytime that the level resets, all the variables resets. Any ideas? The script is in C#. Here is my script:
using UnityEngine;
using System.Collections;
public class Resetter : MonoBehaviour {
public Rigidbody2D projectile; // The rigidbody of the projectile
public float resetSpeed = 0.025f; // The angular velocity threshold of the projectile, below which our game will reset
public Sprite newSprite;
private float resetSpeedSqr; // The square value of Reset Speed, for efficient calculation
private SpringJoint2D spring; // The SpringJoint2D component which is destroyed when the projectile is launched
void Start ()
{
// Calculate the Resset Speed Squared from the Reset Speed
resetSpeedSqr = resetSpeed * resetSpeed;
// Get the SpringJoint2D component through our reference to the GameObject's Rigidbody
spring = projectile.GetComponent <SpringJoint2D>();
}
void Update () {
// If we hold down the "R" key...
if (Input.GetKeyDown (KeyCode.R)) {
// ... call the Reset() function
Reset ();
}
// If the spring had been destroyed (indicating we have launched the projectile) and our projectile's velocity is below the threshold...
if (spring == null && projectile.velocity.sqrMagnitude < resetSpeedSqr) {
// ... call the Reset() function
Reset ();
}
}
void OnTriggerExit2D (Collider2D other) {
// If the projectile leaves the Collider2D boundary...
if (other.GetComponent<Rigidbody2D>() == projectile) {
// ... call the Reset() function
Reset ();
}
}
void Reset () {
// The reset function will Reset the game by reloading the same level
Application.LoadLevel (Application.loadedLevel);
}
}
Comment