Okay… so, when I play Level One, every time I hit a block, it updates the score as normal. But when I complete the level and move on to Level 2, the score becomes my placeholder text and won’t update when I hit a block. It updates in the inspector, but not the actual game graphics itself, so I’m sure it’s a problem with the TextMeshPro text.
Here’s my code for GS (GameStatus) and Block:
Block:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Block : MonoBehaviour {
// Cached reference
Level level;
GS gs;
private void Start()
{
level = FindObjectOfType<Level>();
level.CountBreakableBlocks();
}
private void OnCollisionEnter2D(Collision2D collision)
{
Destroy(gameObject, .1f);
level = FindObjectOfType<Level>();
level.BlockDestroyed();
gs = FindObjectOfType<GS>();
gs.AddScore();
}
}
and GS:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class GS : MonoBehaviour
{
[Range(0.1f, 10f)] [SerializeField] float gameSpeed = 1f;
[SerializeField] int newScore = 79;
[SerializeField] int score = 0;
[SerializeField] TextMeshProUGUI scoreText;
private void Awake()
{
int scoreCount = FindObjectsOfType<GS>().Length;
if (scoreCount > 1)
{
gameObject.SetActive(false);
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
}
}
// Start is called before the first frame update
private void Start ()
{
scoreText.text = score.ToString();
}
// Update is called once per frame
void Update ()
{
Time.timeScale = gameSpeed;
}
public void AddScore ()
{
score += newScore;
scoreText.text = score.ToString();
}
}