简体   繁体   English

Unity3D碰撞怪异行为

[英]Unity3D collision strange behaviour

Something must be wrong with this code because I put Debug.Log into the collision first to make sure it worked and it worked perfectly. 这段代码肯定有问题,因为我首先将Debug.Log放入冲突中,以确保它可以正常工作。 The problem is that the score acts irregularly, sometimes increasing by 20, sometimes decreasing by 20, and sometimes staying the same. 问题是分数行为不规则,有时会增加20,有时会减少20,有时保持不变。 I must be making some stupid mistake that I can't see. 我一定犯了一些我看不到的愚蠢错误。

public int score = 0;

void OnCollisionEnter (Collision col)
{
    if(col.gameObject.name == "Plane")
    {
//On collision, add 10 to score, access text, change text to score. 
score += 10;
    TextMesh ScoreMesh = GameObject.Find("Score").GetComponent<TextMesh>();
        ScoreMesh.text = score.ToString ();
    }
}

Is score+=10 like that in the code you run? 在您运行的代码中,score + = 10是否像这样? If so, it needs to be in the if statement. 如果是这样,它必须在if语句中。 Then you need to make the scoremesh a variable, right now you have two types, TextMesh and ScoreMesh , and no variable, so make it one type and a variable. 然后,您需要使scoremesh成为变量,现在您有两种类型, TextMeshScoreMesh ,并且没有变量,因此使其成为一种类型和变量。 You would then replace ScoreMesh.text with whatever.text 然后,您可以使用whatever.text ScoreMesh.text替换ScoreMesh.text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM