Hello. I want to create counter in my 2D game. When player touching object (tree), script add 1 money(for example) to it. In real counter adding 1 money every frame. I don't know how to fix this. May be this problem with code? Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TextCounter : MonoBehaviour { [SerializeField]public static float moneyCount; Text count; void Start(){ count = GetComponent<Text> (); } void Update(){ count.text = "Money: " + moneyCount.ToString(); } }
You need to have it occur every time the object intersects with the tree's collider. So use an OnTriggerEnter2D function. In that function you can determine if the object that intersected with the tree's collider is the player, then increase score or moneyCount. Code (CSharp): void OnTriggerEnter2D(Collider2D other) { if(other.gameObject.tag == "Player") { moneyCount++; } } Something like that. Check out the OnTriggerEnter2D page: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html Or, check out a video on creating a score/counter system: