【问题标题】:Unity 2018 - UI Text Element - Not Updating Count Upon PickupUnity 2018 - UI 文本元素 - 拾取时不更新计数
【发布时间】:2018-11-15 23:28:46
【问题描述】:

我的问题是代码只适用于玩家触摸的第一个项目(星形平板电脑)。之后,数字将保持为 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;
    }    
}

【问题讨论】:

  • starTxt 是如何赋值的?
  • @Draco18s startTxt.text = startTabCollected + " / " + starTabCount;
  • 那是starTxt.textstarTxt的成员
  • @Draco18s 我已通过检查器将 UI 元素文本插入到 startTxT 变量中。 starText.text 允许我调整starText 内部的文本。所以我很困惑你想在这里告诉我什么。

标签: c# user-interface unity3d


【解决方案1】:

场景中有三个脚本副本

每个副本都有这个:

private int starTabCollected;

这意味着您选择的每一个总是第一个。

【讨论】:

  • StarTabCount 工作正常,因为它从数组中读取到场景中并且是一次性变量。基本上,它只是告诉玩家关卡中有多少对象。 starTabCollected 是问题所在。游戏开始文字为:0/20(假设生成了 20 颗星)。我拿起第一颗星星,上面写着:1/20。但是,它永远不会再次更新;它始终是 1/20,甚至 Debug.Log() 每次拾取时都读取“1”。我假设变量不是递增而是一次。
  • 好的,我明白你现在在说什么了。每个副本都获得相同的脚本。这是导致错误的原因。如何将其修复到每次都会读取新的位置?
  • 同样的事情适用于starTabCollected,是的。您需要将这些值保存在一个单一的中心位置。通常是另一个脚本。
  • 请注意,static 并不总是解决方案。是的,静态允许您在任何地方引用字段表单,但这意味着该值只有 一个副本。在这种情况下,这很好,因为您只想要一份。
  • 您可以使用GetComponent&lt;?&gt;() 获取对附加到游戏对象的脚本的引用。
猜你喜欢
  • 1970-01-01
  • 2021-01-07
  • 2019-06-16
  • 2016-12-26
  • 1970-01-01
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 2020-06-09
相关资源
最近更新 更多