无法在Unity中添加分数

huangapple go评论47阅读模式
英文:

Can't add score in Unity

问题

I'm trying to add +1000 to a score when my player hit an item (I'm trying to do an endless runner game).
I have in my game manager the following code:

    private float currentScore = 0;
    void Update(){
        if(!isGameEnded){
            currentScore = Time.timeSinceLevelLoad * 10f;
            if(scoreBonus){
                currentScore += 1000;
                scoreText.SetText(currentScore.ToString("0"));
                scoreBonus = false;
            }
            scoreText.SetText(currentScore.ToString("0"));
        }
    }

And I have an itemCollision script in which I update my GameManager's scoreBonus variable:

    void OnCollisionEnter(Collision collisionInfo){
        if(collisionInfo.collider.tag == "Player"){
            FindObjectOfType<PlayerCollision>().ActivateInvicibility(invisibilityDuration);
            FindObjectOfType<GameManager>().scoreBonus = true;
            Destroy(gameObject);
        }
    }

The thing is when I hit the item, the score updates really fast, I can see the +1000 happened and then it disappears. I've tried to log the score, but everything seems normal. When it hits an item, its value changes in the log but not in my score text.

英文:

I'm trying to add +1000 to a score when my player hit an item (I'm trying to do an endless runner game).
I have in my game manager the following code:

    private float currentScore = 0;
    void Update(){
        if(!isGameEnded){
            currentScore = Time.timeSinceLevelLoad * 10f;
            if(scoreBonus){
                currentScore += 1000;
                scoreText.SetText(currentScore.ToString(&quot;0&quot;));
                scoreBonus = false;
            }
            scoreText.SetText(currentScore.ToString(&quot;0&quot;));
        }
    }

And I have an itemCollision script in which I update my GameManager's scoreBonus variable:

    void OnCollisionEnter(Collision collisionInfo){
        if(collisionInfo.collider.tag == &quot;Player&quot;){
            FindObjectOfType&lt;PlayerCollision&gt;().ActivateInvicibility(invisibilityDuration);
            FindObjectOfType&lt;GameManager&gt;().scoreBonus = true;
            Destroy(gameObject);
        }
    }

The thing is when I hit the item, the score update really fast, I can see the +1000 happened and then it disappear. I've tried to log the score but everything seems normal, when he hits an items, its value changes in the log but not in my score text.

答案1

得分: 2

这个问题是这一行:

currentScore = Time.timeSinceLevelLoad * 10f;

这将覆盖对当前分数所做的任何更改,因为分数是计算的结果。

为了保持简单,您可以创建另一个变量来存储额外的得分点,然后在显示时将它们相加。

private float currentScore;
private float bonusScore;

..

if (scoreBonus)
{
    bonusScore += 1000;
}

scoreText.SetText((currentScore + bonusScore).ToString("0"));
英文:

This issue is this line here

currentScore = Time.timeSinceLevelLoad * 10f;

This overrides any change you make to current score, since the score is the result of a calculation.

To keep things easy, you can make another variable to store additional score points, then add the two together when displaying.

private float currentScore;
private float bonusScore;

..

if (scoreBonus)
{
    bonusScore += 1000;
}

scoreText.SetText((currentScore + bonusScore).ToString(&quot;0&quot;));

huangapple
  • 本文由 发表于 2023年4月11日 04:11:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定