Now that I got my game working again after having the heart-attack of noting working (thanks to Nina for the tips) I have been busy with the extreme tunning and have added various features. Such as lives, coins, a cursor hide option, and moving unbreakable blocks to my game. I also have it to where every 250 coins you get an extra life (still trying to find a good number for the extra life). However, my coin text for some reason does not do live updates and will only update after the current play session is over. I also have a problem with as soon as you gain one extra life you start to get an extra life after every coin you collect. I would love for someone to look at my code and tell me what might be going wrong. So if you have the time here is my code:
GameSession.cs
// for the coins to lives
public static int coinsForLevelUp;
public static int coinIntialValue;
[SerializeField] private int coinIntervalForLevelUp = 250;
public static bool firstLevelLoaded = true;
private void Start()
{
scoreText.text = currentScore.ToString();
if (firstLevelLoaded)
{
coinsForLevelUp = coinIntervalForLevelUp;
coinIntialValue = coinIntervalForLevelUp;
print("Coins needs for level up is " + coinsForLevelUp);
firstLevelLoaded = false;
}
CountCoins(Paddle.coinCount);
CountLives(LoseCollider.numLives);
}
// Update is called once per frame
void MakeInstance()
{
if (instance == null)
{
instance = this;
print("Making instance");
}
Time.timeScale = gameSpeed;
}
public void CountLives(int numLives)
{
lifeCount.text = "Lives " + numLives;
}
public void CountCoins(int numCoins)
{
if (numCoins >= coinsForLevelUp)
{
LoseCollider.numLives++;
coinIntervalForLevelUp += coinIntialValue;
AudioSource.PlayClipAtPoint(levelUpSound, transform.position, 1f);
CountLives(LoseCollider.numLives);
}
coinCount.text = "Coins " + numCoins;
}
Paddle.cs
public int minCoinValue = 10, maxCoinValue = 20;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Collectibles")
{
int coinValue = Random.Range(minCoinValue, maxCoinValue);
coinCount += coinValue;
print("Collected: " + coinCount);
GameSession.instance.CountCoins(coinCount);
AudioSource.PlayClipAtPoint(coinSound, transform.position, 1f);
}
Destroy(collision.gameObject);
}
Lose Collider
public static int numLives;
public static bool firstLevelLoaded = true;
[SerializeField] private AudioClip ballLostSound;
void Start()
{
if(firstLevelLoaded)
{
numLives = 3;
firstLevelLoaded = false;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag != "Collectibles")
{
numLives--;
if (numLives < 0)
{
SceneManager.LoadScene("Game Over");
Paddle.firstLevelLoaded = true;
firstLevelLoaded = true;
GameSession.firstLevelLoaded = true;
}
else
{
GameSession.instance.CountLives(numLives);
Ball.instance.SetBallPosition();
}
AudioSource.PlayClipAtPoint(ballLostSound, transform.position, 1f);
}
if(collision.tag == "Collectibles")
{
Destroy(collision.gameObject, 1f);
}
}
The coin text is just like the score text and I set it up the same so I am not sure why it is not working. Thank you in advance for any help you can give.