简体   繁体   English

Unity 2018-UI文本元素-拾取后不更新计数

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

My problem is that the code will only work the first item (Star Tablet) that the player touches. 我的问题是该代码仅适用于玩家触摸的第一项(Star Tablet)。 After that, the number will remain 1. I suspect that my Destroy() function erases the script and the count must be forgotten? 之后,数字将保持为1。我怀疑我的Destroy()函数会删除脚本,并且必须忘记计数吗? However, I don't know of any alternative measures to take. 但是,我不知道要采取其他替代措施。 If I'm wrong about that, please inform me of what is going wrong and what steps I'd need to fix it. 如果我错了,请告知我出了什么问题以及需要解决的步骤。 Here is my entire script: 这是我的整个脚本:

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;
    }    
}

You have three copies of your script in the scene 您在场景中有三个脚本副本

Each copy has this: 每个副本都有:

private int starTabCollected;

This means that each one you pick up is always the first one. 这意味着您选择的每个人始终都是第一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM