簡體   English   中英

Unity 2018-UI文本元素-拾取后不更新計數

[英]Unity 2018 - UI Text Element - Not Updating Count Upon Pickup

我的問題是該代碼僅適用於玩家觸摸的第一項(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;
    }    
}

您在場景中有三個腳本副本

每個副本都有:

private int starTabCollected;

這意味着您選擇的每個人始終都是第一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM