Applying in-game currency

I am also trying to add an in-game currency and a sprite store. in my version of the game,when you finish all the levels (there are 10 levels) you will be awarded with 10 coins. then you can enter a item store with multiple stripes for the ball. I have a problem however in awarding 10 coins to the player as I do not know how to change my if statement to a bool

‘’’
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class CurrencyController : MonoBehaviour {
public LevelManager LevelManager;
public int coins;

 public Text coinsDisplay;

 void Awake ()
 {
     UpdateCurrencyUI ();
 }

 void Update ()
 {
     AddCurrency (10);
 }

 void AddCurrency (int amount)
 {
	if (LevelManager.LoadLevel("Congrats")){
 
	coins += amount;
     Debug.Log ("You now have " + coins.ToString() + " coins!");
     UpdateCurrencyUI ();
	}
	}

 void RemoveCurrency (int amount)
 {
     coins -= amount;
     UpdateCurrencyUI ();
 }

 void UpdateCurrencyUI ()
 {
     coinsDisplay.text = coins.ToString();
 }

}’’’

it does not recognize the" if (levelmanager.loadlevel) code

Hi Fadi,

I have moved this post to it’s own topic as it was not related to the subject matter in the other topic.

Regarding the issue, this is going to be because your LevelManager has not been initialised, have you remembered to drag it into this script from the Hierarchy?

Also, you should really get in the habbit of not spelling your variables exactly the same as your classes, for example;

public LevelManager LevelManager;

When looking at your code, a line like this;

LevelManager.LoadLevel("Congrats")

…would typically indicate that the LoadLevel method was static and belonged to the class LevelManager.

Instead, I would recommend using a convention as follows;

public LevelManager levelManager;

The lowercase “L” now indicates that this is a variable/object rather than a class.

So from;

levelManager.LoadLevel("congrats");

…I can tell that the method LoadLevel is related to the instance of the class/type LevelManager called levelManager.

By using this convention consistently it will help to make your code more readable and you will invariably learn to identify issues yourself as you progress.

Hope this helps :slight_smile:

1 Like

Privacy & Terms