我在场景中有一个GUI,上面写着“收集的胶囊:0/10”,还有一个胶囊对象,其中有对撞机,只要玩家进入胶囊,胶囊就会被摧毁,收集的胶囊将增加1。
销毁效果很好,没有显示GUI。我的代码有什么问题?
这是我的代码,我在Player本身上分配了此C#脚本:
using UnityEngine;
using System.Collections;
public class CapsuleGET : MonoBehaviour {
int capscore=0;
void Start(){
}
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
capscore =capscore+1;
}
void Update(){
GUILayout.Label("Capsules Collected: "+capscore+"/10");
}
}
最佳答案
这样..非常容易
using Unity.UI;
public class CapsuleGET
public Text displayScore; // DRAG to connect in Editor
void OnTriggerEnter(Collider other) {
Destroy(other.gameObject);
capscore =capscore+1;
displayScore.text = capscore.ToString();
1-单击以添加画布(不要忘记选择“根据屏幕尺寸缩放”)
2-单击以添加文本,并根据需要放置位置。
3-在您的脚本中,“公共文字分数”
4-从“文本”拖动到该变量
explanation with diagrams