Extreme Tuning

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.

From the looks of the code you never reset numCoins to 0 so after it reaches 250 it will always be higher than coinsForLevelUp which is why after the first extra life every coin you collect afterwards is adding a life.

How would I reset it? I tried numCoins = 0 but that keeps the coin text at 0 (finally got the coin text to update after going through my prefabs).

It depends how you want to have the game play, upon collecting enough coins for a new life most games reset the displayed coins to zero but if you want the coins to persist just add a new int variable.

So you have one coin variable that gets displayed to the player and one that is strictly for adding an extra life that doesn’t get displayed, that would be the easiest way.

Actually rather than making a new variable just increasing coinsForLevelUp inside of the if statement after they reached 250 coins would work better, if you wanted the coins to persist.

If you want to reset it to 0 what you were doing should work as long as you were setting it to 0 inside of the if statement. Then upon collect another coin it should start counting/displaying from 0 again.

Yeah sorry, I did not write my reply clearly. I like the reset to 0 but the problem is that it goes to 0 but then as soon as I get another coin it acts as the number of coins never reset. So it will go from anything over 250 back to 0 but then I get another coin and it jumps to like 265 (or what the value of the coin I just got was added to what I had before the reset).
Here is what I have been trying:

    public void CountCoins(int numCoins)
    {
        int coinResest = 0;
        if (numCoins >= coinsForLevelUp)
        {
            LoseCollider.numLives++;
            coinIntervalForLevelUp += coinIntialValue;
            AudioSource.PlayClipAtPoint(levelUpSound, transform.position, 1f);
            CountLives(LoseCollider.numLives);
            numCoins = coinResest;
            coinsForLevelUp = coinIntervalForLevelUp;
        }
        coinCount.text = "Coins " + numCoins;
    }

I tried a while statment where the if is but I got the same result. I also tried various different ways of using the vairables. I am probably missing something rather simple but I cannot figure it out. Thank you for the adivce as it has saved my coin idea so far.

I’m heading to sleep now but I think the problem may be coinCount as coinCount is being added to in a different script and keeps its value. I has a similar issue of my score resetting to 0 for my rolling score text. Which I fixed with a third variable. Current score, currentdisplayed score and display score. Anyway I’ll look at mine for a reference tomorrow.

1 Like

Yes, the problem looks to be variable scope. You pass in numcoins by value so resetting to zero is local to the method. You should setup a setter wherever numcoins is so you can call something like:

GameManager.SetNumCoins(coinReset);

You could also try subtracting 250 coins instead of resetting to zero.

Wasn’t able to crash so I’ll add on to my last reply, so int the paddle script you have the variable coinCount which is getting added to.

GameSession.Instance.CountCoins(coinCount); and this line of code is saying that coinCount is to be added/changed/turned to numCoins inside of the game session script.

So even though we reset numCoins to 0 coinCount is still at 250 so in short we need to reset coinCount to 0.

1 Like

Thanks for the tips, I am starting to go in and apply what you said. Will update you on how it goes.

Alright, thanks to your help I was able to get it working for the reset (at least I have not found any bugs yet).
I did this to my paddle scrip:

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Collectibles")
        {
            int coinValue = Random.Range(minCoinValue, maxCoinValue);
            coinCount += coinValue;
            print("Collected: " + coinCount);
            GameSession.instance.CountCoins(coinCount);
            while (coinCount >= 250)
            {
                GameSession.instance.CountLives(LoseCollider.numLives++);
                coinCount = 0;
            }
            AudioSource.PlayClipAtPoint(coinSound, transform.position, 1f);
        }
        Destroy(collision.gameObject);
    }

Thank you so much!!!

:+1:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms