Hello.
I’m coding a little 2D game and I want to check if the cash has changed. I tried various methods but don’t work properly.
My idea is get a boolean flag to tell other scripts that the cash has changed, then maintain, increment or decrement their int variables in “if” statements.
The code that manages the cash in the entire game is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GlobalCash : MonoBehaviour
{
public static int CashCount;//Where other scripts adds or takes cash
public GameObject CashDisplay;//Text
public int InternalCash;//Real cash count
// Update is called once per frame
void Start()
{
}
void Update()
{
InternalCash = CashCount;
if(InternalCash < 0)
{
InternalCash = 0;
}
CashDisplay.GetComponent<Text>().text = "Stable Coin: " + InternalCash;
}
}
You may want to just work through two or three different tutorials where they display a number to get a feel for what’s happening.
If you have actual errors, the complete error message contains everything you need to know to fix the error yourself.
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors.
The important parts of the error message are:
the description of the error itself (google this; you are NEVER the first one!)
the file it occurred in (critical!)
the line number and character position (the two numbers in parentheses)
also possibly useful is the stack trace (all the lines of text in the lower console window)
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.
Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
It’s basically a function wrapper around a variable, allowing you to get and set it.
Which in turn gives you control over what else is supposed to happen when you get or set the value.
So you may simply execute some other code whenever the variable is set to a value it does not currently have.