私は弾丸を持っています。標的に当たったらスコアが1ずつ増加しますが、スコアは2増加します。弾丸は、ターゲットは弾丸1を増やす必要があるときにスコアが2ずつ増加する
public class Bullet : MonoBehaviour {
float lifespan = 2;
void Start()
{
// destroy the bullet
Destroy(gameObject, lifespan);
}
void OnTriggerEnter(Collider other) //collider event
{
if (other.gameObject.CompareTag("Score"))
{
Score.score = Score.score + 1;
}
}
}
スコアコードのコライダーとシリンダとrigibody
コードは、私が前にこの正確な問題を解決してきましたが、私が検索
public class Score : MonoBehaviour {
public static int score; // The player's score.
Text text; // Reference to the Text component.
void Start()
{
// Set up the reference.
text = GetComponent<Text>();
// Reset the score.
score = 0;
}
void Update()
{
// Set the displayed text to the score value.
text.text = "Score: " + score;
}
}
デバッガを使用し、メソッド「OnTriggerEnter」が2回起動されているかどうかを確認します。 –
これを見てください:https://forum.unity3d.com/threads/ontriggerenter-is-called-twice-sometimes.95187/また、オブジェクトのいずれかに2つのコライダーがあるかどうか確認してください。 – pasotee