我有一顆子彈,當它達到目標時,它應該增加1分,但得分增加2.子彈是一個膠囊,有對撞機和rigibody並且目標是用對撞機缸和rigibody得分提高2點,應該提高1點
代碼上子彈
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;
}
}
}
比分代碼
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'是否啓動兩次。 –
請看看這個請:https://forum.unity3d.com/threads/ontriggerenter-is-called-twice-sometimes.95187/另外,請檢查是否有任何對象的2個碰撞器請。 – pasotee