Hi again! Good news! I was able to replicate the issue and fixed it!
First of all, change your GameplayController script to this, you’ll get an error, this is just for clarity, that script was doing instances and other things that weren’t required, that could also be part of the solution, not 100% sure, but it never hurts to keep your code as clean as possible.
using UnityEngine;
using TMPro;
public class GameplayController : MonoBehaviour
{
[SerializeField] private TextMeshProUGUI lifecount;
[SerializeField] Lives lives;
void Start()
{
CountLives();
}
public void CountLives()
{
lifecount.text = "Lives: " + lives.numLives;
}
}
To fix the error after those changes, this is in your LoseColider script:
private void OnTriggerEnter2D(Collider2D collision)
{
lives.numLives--;
if (lives.numLives <=0)
{
SceneManager.LoadScene("Game Over");
}
else
{
FindObjectOfType<GameplayController>().CountLives(); //Change the error to this
Ball.ballInstance.SetBallPosition();
}
AudioSource.PlayClipAtPoint(ballLostSound, transform.position, 1f);
}
And now to fix the error, let me explain what happened. There are two common issues as to why things might work in the editor but not in the final build, as Nina explained, the library might be an issue, if that doesn’t solve anything then it’s because there’s some racing going on. Racing is when two things are running at the “same time” but they shouldn’t run at the “same time”, but instead they should run in a specific order.
In this case, the code tried to add a new life but also update the text at the same time, so sometimes it work, sometimes it didn’t. To fix you’ll have to change the order in which those scripts are executed. To do this just change the Start method to an Awake method in your LoseColider script.
private void Awake() //Instead of Start use Awake
{
if (SceneManager.GetActiveScene().name == "Level 1")
{
lives.numLives = 3;
}
else
{
lives.numLives += 1;
}
}
That’s it, that should solve your issues.
I also have some bad news, I found some other issues with the Pause menu and the ball. When I open the Pause Menu and go to the Main menu from there, the game will still be paused, we don’t want that, that’s an easy fix, I’ll leave that to you.
There’s also an issue with the ball, it might end up in an infinite loop, I was able to solve that by setting the ball’s gravity scale to 0.5 instead of 1.
If you are still having issues I’ll send you the version I have as a package, you’ll just have to order the levels in the builds settings.