【发布时间】: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