该计数器会随机增加。这让我疯狂。OnTriggerEnter被多次调用
当玩家拿起关卡(10)上的所有游戏对象时,关卡应该重新开始。
然而这一工程,用随机拿起对象也可以添加+1的成绩,这是预期,或+2。
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Pickup : MonoBehaviour {
//Required Variables
public GameObject pointsObject;
public Text scoreText;
private int score;
private int scoreCount;
void Start()
{
score = 0;
SetScoreText();
scoreCount = GameObject.FindGameObjectsWithTag("Pickups").Length;
}
void Update()
{
if(score >= scoreCount)
{
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);
}
SetScoreText();
Debug.Log("Found " + scoreCount + " Pickup Objects!");
}
void OnTriggerEnter(Collider col)
{
if(col.gameObject.CompareTag("Pickups"))
{
Destroy(col.gameObject);
score++;
}
}
void SetScoreText()
{
scoreText.text = "Score: " + score.ToString();
}
}
任何人都可以看到为什么我有这个问题,因为我看不出为什么。提前致谢。
add Debug.Log(“yo”+ gameObject.name);到OnTriggerEnter – Fattie
Ben,一旦你开始编程更多,“分数”应该是一个具有“set”动作的属性,只需改变那里的文本。干杯 – Fattie