-2

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)

Picture of game display

Multiplier game object

Gold game object

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;
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • sounds like the multiplierui reference is not set in the editor. double check you dragged your ui element into that variable slot. – hijinxbassist Nov 09 '21 at 18:40
  • @hijinxbassist I think I did that right. I posted a picture of the element here: https://i.stack.imgur.com/k5un2.png – Jim Barron Nov 09 '21 at 18:50
  • Are you sure you are looking at the correct instance of `game`? Put a `Debug.Log(name, this);` on the beginning of `Update` .. does it print the expected name of the object? And if you click on the log message does it highlight the correct object in the hierarchy and are the references there in the Inspector on that object? – derHugo Nov 09 '21 at 19:34
  • In your screenshots there are two instances: One on Gold object and one on Multiplier object .. in the first only the multiply UI is referenced in the other one only the goldUi ... The other one is null respectively – derHugo Nov 09 '21 at 19:36
  • Debug worked and it does highlight the correct UI elements when I click on them. https://imgur.com/a/fg91rqN – Jim Barron Nov 09 '21 at 19:51
  • @JimBarron as I said: In your two screenshots in your question I can see that you have **two** instances of `game`. On `golddisplay` you have only the `goldui` referenced, the `multiplierui` is **`null`**. Wise versa on the `multiplierdisplay` you hav only the `multiplierui` referenced, here the `goldui` is **`null`** ... I hope you now see the problem ;) ... You only want **ONE** central instance of `game` and reference both your texts there instead! – derHugo Nov 10 '21 at 09:03

1 Answers1

0

The NullReferenceException error clearly explains that the goldui and multiplierui are empty in the inspector. You have to drag them from the Hierarchy and add them to the inspector.

Shem Tom
  • 56
  • 1
  • 9
  • I have them assigned. I assigned gold to gold and multiplier to multiplier. When I assigned them both to one UI element, it worked! But I still get the error. https://imgur.com/a/xc5UvQQ – Jim Barron Nov 09 '21 at 19:22
  • I got it to work by assigning both text elements to one game object in the inspector. Then the other one I removed the script completely. I still get a ton of errors about NullReferenceException but both of the UI elements are now updating. https://imgur.com/a/y6RiFJr – Jim Barron Nov 09 '21 at 19:54
  • can I see the errors! – Shem Tom Nov 09 '21 at 20:03
  • What I would suggest is creating an empty object in the hierarchy which will hold the game script and manage or update values for your UI(Text etc). – Shem Tom Nov 09 '21 at 20:06
  • https://imgur.com/a/fg91rqN – Jim Barron Nov 09 '21 at 20:06
  • Creating the empty object makes it much simpler, that's a good idea and it's working perfectly now. It turns out I had accidentally attached my script to another object in the Hierarchy called "game", but never assigned the text elements to it. Once I removed the script from that game object the error went away. THANK YOU so much!! – Jim Barron Nov 09 '21 at 20:08