I'm making a 2D platformer in Unity. The TextMeshPro element that tracks the player's score is incrementing by 10 every time the player interacts with the pickup. This works for the first level but after the OnTriggerEnter2D is called in the LevelExit script, the playerLives variable continues to be connected with the TMPro element and updates upon death, but the score int doesn't update any further than the first level. It does work, however if I start the game from the second level. So it must be something to do with the scene loading.
I have tried sticking debuglogs everywhere and made a public method called ResetScore to update it. The score does in fact reset to 0 after the next level loads, if printing to console I can see this. But it still doesn't show in Unity.
ScoreKeeper Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class ScoreKeeper : MonoBehaviour
{
Pickups pickups;
private int score = 0;
public int pickupCount = 0;
[SerializeField] int playerLives = 3;
[SerializeField] TextMeshProUGUI livesText;
[SerializeField] TextMeshProUGUI scoreText;
void Awake()
{
int numScoreKeepers = FindObjectsOfType<ScoreKeeper>().Length;
if (numScoreKeepers > 1)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
void Start()
{
score = 0;
livesText.text = playerLives.ToString();
scoreText.text = score.ToString();
}
public void ProcessPlayerDeath()
{
if (playerLives > 1)
{
TakeLife();
}
else
{
ResetGameSession();
}
}
public void ResetGameSession()
{
FindObjectOfType<ScenePersist>().ResetScenePersist();
SceneManager.LoadScene(0);
Destroy(gameObject);
}
void TakeLife()
{
playerLives --;
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex);
livesText.text = playerLives.ToString();
}
public void IncrementScore(int value)
{
score += value;
scoreText.text = score.ToString();
Debug.Log("scoreText.text is: " + scoreText.text);
}
public bool CheckIfHasPickups()
{
pickupCount = GameObject.FindGameObjectsWithTag("Pickups").Length;
if (score >= pickupCount * 10)
{
return (true);
}
else
{
return (false);
}
}
public void ResetScore()
{
score = 0;
Debug.Log("Score is " + score);
}
}
Pickups Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pickups : MonoBehaviour
{
ScoreKeeper scoreKeeper;
Rigidbody2D myRigidbody;
void Start()
{
scoreKeeper = FindObjectOfType<ScoreKeeper>();
myRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
PickupInteraction();
}
public void PickupInteraction()
{
if (myRigidbody.IsTouchingLayers(LayerMask.GetMask("Player")))
{
scoreKeeper.IncrementScore(10);
Destroy(gameObject);
}
}
}
ScenePersist Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScenePersist : MonoBehaviour
{
void Awake()
{
int numScenePersists = FindObjectsOfType<ScenePersist>().Length;
if(numScenePersists > 1)
{
gameObject.SetActive(false);
Destroy(gameObject);
Debug.Log("Level Persist sees it is a copy, destroys itself");
}
else
{
DontDestroyOnLoad(gameObject);
}
}
public void ResetScenePersist()
{
Destroy(gameObject);
}
}
LevelExit Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelExit : MonoBehaviour
{
ScoreKeeper scoreKeeper;
void Start()
{
scoreKeeper = FindObjectOfType<ScoreKeeper>();
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player" && scoreKeeper.CheckIfHasPickups())
{
StartCoroutine(LoadLevel());
}
else
{
Debug.Log("Not all pickups collected!");
}
}
IEnumerator LoadLevel()
{
yield return new WaitForSecondsRealtime(1);
scoreKeeper.ResetScore();
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
SceneManager.LoadScene(currentSceneIndex + 1);
FindObjectOfType<ScenePersist>().ResetScenePersist();
}
}
DebugLog on almost every method, this has proven that the scripts are working as intended, updating the score variable to 0 after the level resets. I have no idea why this isnt working.
I have also tried to ForceUpdate the canvas, all variations of syntax to update the TMPro element.