For some reason upon finishing a level the lives counter freezes and remains on whatever life count you had in level 1. even if I start at level 2 it will freeze and I am unable to deplete the lives counter.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
public class GameSession : MonoBehaviour
{
int currentSceneIndex;
[SerializeField] int italianlives = 3;
// Start is called before the first frame update
void Awake()
{
currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int gameSessionObjCount = FindObjectsOfType<GameSession>().Length;
if (gameSessionObjCount > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
public void DeathProcess()
{
if(italianlives > 1)
{
TakeLife();
}
else
{
ResetSession();
//aka gameover
}
}
// Update is called once per frame
void Update()
{
}
void TakeLife()
{
italianlives -= 1;
SceneManager.LoadScene(currentSceneIndex);
}
void ResetSession()
{
SceneManager.LoadScene(0);
Destroy(gameObject);
}
}
And here’s my player script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Animations;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float moveSpeed;
[SerializeField] float jumpSpeed = 25f;
[SerializeField] float climbingSpeed = 2f;
Vector2 moveInput;
Rigidbody2D myBody;
Animator anims;
BoxCollider2D hitBox;
[SerializeField] float italianGravity = 10f;
[SerializeField] float landingTime = 0.2f;
bool isAlive;
[SerializeField] GameObject fireBall;
[SerializeField] Transform gun;
GameSession gameSession;
// Start is called before the first frame update
void Start()
{
gameSession = FindObjectOfType<GameSession>();
isAlive = true;
anims = GetComponent<Animator>();
myBody = GetComponent<Rigidbody2D>();
hitBox = GetComponent<BoxCollider2D>();
myBody.gravityScale = italianGravity;
}
// Update is called once per frame
void Update()
{
GameOver();
//if (!isAlive) { return; }
Run();
FlipIt();
ClimbLadder();
}
void FlipIt()
{
bool touchingGround = hitBox.IsTouchingLayers(LayerMask.GetMask("Platforms"));
bool touchingLadder = hitBox.IsTouchingLayers(LayerMask.GetMask("Climbing"));
bool playerMoving = Mathf.Abs(myBody.velocity.x )> Mathf.Epsilon && touchingGround;
if (playerMoving && isAlive)
{
transform.localScale = new Vector2(Mathf.Sign(myBody.velocity.x), 1f);
anims.SetBool("isRunning", true);
}
else
{
anims.SetBool("isRunning", false);
}
anims.SetBool("isJumping", !touchingGround && !touchingLadder && isAlive);
}
private void Run()
{
if (isAlive)
{
Vector2 playerVelocity = new Vector2(moveInput.x * moveSpeed, myBody.velocity.y);
myBody.velocity = playerVelocity;
}
else { return; }
}
private void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
Debug.Log("MoveInput");
}
void OnJump(InputValue value)
{
IEnumerator coroutine = Jumping(value);
StartCoroutine(coroutine);
}
IEnumerator Jumping(InputValue value)
{
bool onLadder = hitBox.IsTouchingLayers(LayerMask.GetMask("Climbing"));
bool touchingGround = hitBox.IsTouchingLayers(LayerMask.GetMask("Platforms"));
if (value.isPressed && touchingGround || onLadder && isAlive)
{
myBody.velocity += new Vector2(0f,jumpSpeed);
anims.SetBool("isJumping", true);
}
else { yield break; }
yield return new WaitForSeconds(landingTime);
}
private void ClimbLadder()
{
bool canClimb = hitBox.IsTouchingLayers(LayerMask.GetMask("Climbing"));
bool isMovingUpOrDown = Mathf.Abs(moveInput.y) > Mathf.Epsilon;
if (canClimb)
{
//put a single frame climb sprite anim
myBody.gravityScale = 0f;
myBody.velocity = new Vector2(moveInput.x * moveSpeed, moveInput.y * climbingSpeed);
anims.SetBool("isClimbing", isMovingUpOrDown);
}
else if (!canClimb)
{
//deactive climb anim based on canClimb or just set false here?
anims.SetBool("isClimbing", false);
myBody.gravityScale = italianGravity;
}
}
private void GameOver()
{
if (myBody.GetComponent<CapsuleCollider2D>().IsTouchingLayers(LayerMask.GetMask("Enemies", "Hazards")) || myBody.GetComponent<BoxCollider2D>().IsTouchingLayers(LayerMask.GetMask("Hazards")))
{
StartCoroutine(DeathSequence());
}
else { return; }
}
void OnFire(InputValue value)
{
if (isAlive)
{
Instantiate(fireBall, gun.transform.position, transform.rotation);
}
}
IEnumerator DeathSequence()
{
var cineCam = FindObjectOfType<CameraScript>();
cineCam.KillCam();
isAlive = false;
anims.SetTrigger("isDead");
GetComponent<CapsuleCollider2D>().enabled = false;
GetComponent<BoxCollider2D>().enabled = false;
myBody.velocity = new Vector2(0f, 20f);
yield return new WaitForSecondsRealtime(3);
gameSession.DeathProcess();
}
}