I'm trying to make a simple clicker game in Unity and I have 2 text elements I want to display.
One is the current gold, which is working correctly. The second is the current multiplier, which is not updating at all.
When the player clicks on the character at the center of the screen, their gold will go up by the value of the multiplier (1 to start).
If they buy an upgrade, then the multiplier goes up by 1 and their gold goes down by the cost of the upgrade.
I'm updating the text of the Gold and the Multiplier through the update function but it only works for the Gold display.
Why doesn't the multiplier text get updated?
I'm also getting these errors in the console, which is where I update the text for both game objects.
NullReferenceException: Object reference not set to an instance of an object game.Update () (at Assets/game.cs:42) NullReferenceException: Object reference not set to an instance of an object game.Update () (at Assets/game.cs:43)
Game manager script that declares values of variables:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class script : MonoBehaviour
{
public static int gold;
public static int multiplier;
void Start()
{
gold = 30;
multiplier = 1;
}
}
And here is the code that handles the clicks:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class game : MonoBehaviour
{
public Text goldui;
public Text multiplierui;
public void Increment()
{
script.gold += script.multiplier;
}
public void Buy(int num)
{
if (num == 1 && script.gold >= 25)
{
script.multiplier += 1;
script.gold -= 25;
}
if (num == 2 && script.gold >= 200)
{
script.multiplier += 1;
script.gold -= 200;
}
if (num == 3 && script.gold >= 1800)
{
script.multiplier += 1;
script.gold -= 1800;
}
}
public void Update()
{
goldui.text = "Gold: " + script.gold;
multiplierui.text = "Multiplier: " + script.multiplier;
}
}