using System;
using UnityEngine;
public class CubeScript : MonoBehaviour
{
public static float fallSpeed = 5f;
public static int counter;
public static int moneyGathered = 0;
public event Action ChangeTextToCoinValue;
public void Update()
{
if (transform.position.y < 3.5f)
{
Destroy(gameObject);
CountingHandler();
}
transform.Translate(Vector3.down * fallSpeed * Time.deltaTime);
}
public void CountingHandler()
{
counter = counter++;
if (counter == 3)
{
counter = 0;
moneyGathered++;
if (ChangeTextToCoinValue != null)
{
ChangeTextToCoinValue();
}
}
}
}
using UnityEngine;
using UnityEngine.UI;
public class MoneySystem : MonoBehaviour
{
public Text coinsGatheredValue;
public CubeScript numberofcoins;
public void Start()
{
numberofcoins = FindObjectOfType<CubeScript>();
numberofcoins.ChangeTextToCoinValue += ChangeText;
}
public void ChangeText()
{
coinsGatheredValue.text = numberofcoins.ToString();
}
}
You don’t place the script there. The script gets added to a GameObject in your scene as a Component, the same way you added the Money System component to this object. Then you can drag that GameObject that has the CubeScript component into that slot.
You can even add both components to the same object.
using UnityEngine;
using UnityEngine.UI;
public class MoneySystem : MonoBehaviour {
private Text _coinsGathered;
private void UpdateText() {
_coinsGathered.text = CubeScript.moneyGathered.ToString();
}
private void Update() {
UpdateText();
}
}
That code should work for what you’re trying to do but if you want a more optimized solution though you should do the following instead:
using UnityEngine;
using UnityEngine.UI;
public class MoneySystem : MonoBehaviour {
public Text coinsGathered;
}
and then in CubeScript you would do:
using System;
using UnityEngine;
public class CubeScript : MonoBehaviour {
private const float FallSpeed = 5f;
private static int _counter;
private static int _moneyGathered;
private MoneySystem _moneySystem;
private void Awake() {
_moneySystem = FindObjectOfType<MoneySystem>();
}
private void Update() {
if (transform.position.y < 3.5f) {
CountingHandler();
Destroy(gameObject);
}
transform.Translate(Vector3.down * FallSpeed * Time.deltaTime);
}
private void UpdateCounter() {
if (!_moneySystem || !_moneySystem.coinsGathered) return;
_moneySystem.coinsGathered.text = _moneyGathered.ToString();
}
private void CountingHandler() {
_counter++;
if (_counter < 3) return;
_counter = 0;
_moneyGathered++;
UpdateCounter();
}
}
The first solution is inefficient because it Updates the counter every frame while the second solution is efficient because it only updates the counter when moneyGathered is changed.
Actually making a property for moneyGathered would be a better impliment so you should probably do this:
using System;
using UnityEngine;
public class CubeScript : MonoBehaviour {
private const float FallSpeed = 5f;
private static int _counter;
private static int _moneyGathered;
private static int MoneyGathered {
get => _moneyGathered;
set {
_moneyGathered = value;
UpdateCounter()
}
}
private MoneySystem _moneySystem;
private void Awake() {
_moneySystem = FindObjectOfType<MoneySystem>();
}
private void Update() {
if (transform.position.y < 3.5f) {
CountingHandler();
Destroy(gameObject);
}
transform.Translate(Vector3.down * FallSpeed * Time.deltaTime);
}
private void UpdateCounter() {
if (!_moneySystem || !_moneySystem.coinsGathered) return;
_moneySystem.coinsGathered.text = MoneyGathered.ToString();
}
private void CountingHandler() {
_counter++;
if (_counter < 3) return;
_counter = 0;
MoneyGathered++;
}
}
With this code you wouldn’t touch _moneyGathered you’d only use MoneyGathered that way whenever you change it’s value it automatically updates the counter so you don’t need to worry about doing that anywhere since it’s done for you whenever the value is set.