Корзина — это игровой объект, и это сопровождающий его скрипт:
Код: Выделить всё
using UnityEngine;
public class Basket : MonoBehaviour
{
public AppleScoreCounter scoreCounter;
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Apple"))
{
scoreCounter.GainScore(1);
//do i need to write a destructer class for other???
Destroy(other.gameObject);
Debug.Log("Trigger created by " + other.gameObject.name);
}
}
}
Код: Выделить всё
using UnityEngine;
using TMPro;
public class AppleScoreCounter : MonoBehaviour
{
private TextMeshProUGUI scoreCounter;
public int scorePoints;
private void Awake()
{
Debug.Log("Awake started");
scoreCounter = GetComponent();
Debug.Log("Got past score counter");
}
private void Start()
{
scorePoints = 0;
//UpdateScoreUI();
Debug.Log("Got to end of start");
}
//called when something happens(apple gets caught)
public void GainScore(int points = 1)
{
scorePoints += points;
UpdateScoreUI();
}
private void UpdateScoreUI()
{
Debug.Log("Got to Update and broke");
scoreCounter.text = scorePoints.ToString();
Debug.Log("Score Points " + scorePoints);
}
}
введите здесь описание изображения
Подробнее здесь: https://stackoverflow.com/questions/798 ... ugui-objec
Мобильная версия