If you Restart a Scene normally using
SceneManager.LoadScene();
the Scene must Load again, and this can cause a Bad User Expereience at bigger and more Complex Scenes,
because if it takes 30 Seconds to Load the Scene, it will take 30 Seconds to also Load it again.
I think you can already Imagine, that’s not so good, especially for Games where you die and Respawen often, because you have to see an unnecessary Loading Screen every Time you Respawn.
So how can I Restart a Scene without any Loading Time?
EDIT: My Scene has a few Enemies with Basic Movement (The green Guys), one Question Block from Super Mario Bros (except it only Turns Brown and don’t gives Coins or Score, because Such Variables don’t exist for now), a few Platforms that don’t change at all (the Grey Rectangles),
one Player (the Red Guy)
and a Camera that moves according to the Player Movement.
I don’t think you’ll need to know the last two in order to help me, but just in Case.
Here is a Link to my Mentioned Game
These are the Scripts:
All Scripts are using:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
Enemy:
using UnityEngine.SceneManagement;
public class Enemy1 : MonoBehaviour {
private int health = 1; //Health is not actually used (yet)
public int moveSpeed;
public int moveDirection = 1; //-1 is left, 1 is right
public int currentScene;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (moveSpeed * moveDirection * Time.deltaTime, 0, 0);
}
void OnTriggerEnter2D(Collider2D other){
if (other.CompareTag ("EnemyTurningPoint")){
moveDirection = -moveDirection;
}
else if (other.CompareTag ("Player")) {
SceneManager.LoadScene (currentScene); //Reload Scene
}
}
}
Question Block:
public class QuestionBlock : MonoBehaviour {
public Sprite mySprite;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D other) {
this.GetComponentInChildren<SpriteRenderer>().sprite = mySprite;
}
}
Player Walk:
public class PlayerWalk : MonoBehaviour {
public float moveSpeed;
private float moveInput;
void Start () {
}
void Update () {
//Get Move Input:
if (Input.GetAxis ("Horizontal") > 0) { //Move Right
moveInput = 1;
} else if (Input.GetAxis ("Horizontal") < 0) { //Move Left
moveInput = -1;
} else if ((Input.GetAxis ("Horizontal") > 1) || (Input.GetAxis ("Vertical") < 1)) { //Don't Move
moveInput = 0;
}
transform.Translate (Vector2.right * moveInput * moveSpeed * Time.deltaTime);
}
}
Player Jump:
public class PlayerJumpV3 : MonoBehaviour { //Player Jump Script Iteration 3
public Rigidbody2D rb2d;
public float JumpForce;
public float MaxTime;
private float CurrentTime;
private bool Grounded;
public bool CanHoldJump;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
JumpForce = 30;
MaxTime = 0.25f;
CurrentTime = MaxTime;
Grounded = true;
CanHoldJump = true;
}
// Update is called once per frame
void Update () {
if (CanHoldJump == true)
{
if (CurrentTime >= 0)
{
rb2d.AddForce (new Vector2 (0, Input.GetAxisRaw ("Jump")) * JumpForce * Time.deltaTime, ForceMode2D.Impulse);
CurrentTime -= Time.deltaTime;
}
}
else if (Grounded == false)
{
CanHoldJump = false;
}
if (Grounded == true)
{
CurrentTime = MaxTime;
}
}
private void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
Grounded = true;
CanHoldJump = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
Grounded = false;
}
}
}
Camera:
public class CameraFollow : MonoBehaviour {
public GameObject followTarget;
void LateUpdate () {
transform.position = new Vector3 (followTarget.transform.position.x +2, 0f, -10f);
}
}