Моя проблема в том, что код будет работать только с первым элементом (Star Tablet), к которому прикасается игрок.После этого число останется равным 1. Я подозреваю, что моя функция Destroy () удаляет сценарий и счет должен быть забыт?Тем не менее, я не знаю никаких альтернативных мер, чтобы принять.Если я ошибаюсь по этому поводу, пожалуйста, сообщите мне о том, что идет не так и какие шаги мне нужно исправить.Вот весь мой сценарий:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class StarTabletScript : MonoBehaviour
{
public static GameObject starTab; // The actual Star Tablet gameobject - May need to use in other script later?
private int starTabCount = 0; // Counts total stars in the scene
private int starTabCollected;// Star off with zero star tabs collected
private GameObject[] starTabTotal; // Reads the total amount of star tabs in the scene
public Image starImg; // The star sprite
public Text starTxt; // The star Text
[SerializeField]
private Renderer starMesh; // Used to deactivate the mesh of the star tab upon collision
void Start()
{
starTab = GetComponent<GameObject>();
starTabCollected = 0;
starTabTotal = GameObject.FindGameObjectsWithTag("StarTab");
foreach (GameObject star in starTabTotal)
{
starTabCount++;
}
StartCoroutine(StartShow()); // Shows image upon start
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
starMesh.enabled = false;
StartCoroutine(ShowStar());
}
}
IEnumerator ShowStar() // Shows star image on pickup
{
starTabCollected++;
Debug.Log("3) Stars collected "+starTabCollected);
starImg.enabled = true;
starTxt.enabled = true;
starTxt.text = starTabCollected + " / " + starTabCount;
yield return new WaitForSeconds(3);
starImg.enabled = false;
starTxt.enabled = false;
Destroy(gameObject);
}
IEnumerator StartShow() // Shows star on program start
{
Debug.Log("1) Total Stars in scene "+starTabCount);
Debug.Log("2) StarTab.Length testing "+starTabTotal.Length);
starImg.enabled = true;
starTxt.enabled = true;
starTxt.text = starTabCollected + " / " + starTabCount;
yield return new WaitForSeconds(5);
starImg.enabled = false;
starTxt.enabled = false;
}
}