简体   繁体   English

如何增加分数并将其打印在控制台中?

[英]How do I increase a score and print it in the console?

I'm still trying to finish off my very simple game but I've ran into another problem.我仍在努力完成我非常简单的游戏,但我遇到了另一个问题。 I want the Console to print (" Player's score ") everytime the ball touches the sidewalls.我希望控制台在每次球触及侧壁时打印(“球员得分”)。 So i created a new script and called it ScoreManager that should take care of this function.所以我创建了一个新脚本并将其命名为 ScoreManager 来处理这个功能。 But the only thing the console does is print (0).但控制台唯一能做的就是打印 (0)。 this is the code.这是代码。

void Update()
{
    //boundaries 
    if (Ball.transform.position.x < -5.61f)
    {
        RightScore++;
        print(RightScore);
    }

    if (Ball.transform.position.x > 5.5f)
    {
        LeftScore++;
        print(LeftScore);
    }
            
}

RightScore and LeftScore are not defined. RightScoreLeftScore未定义。 If you define them, it should work:如果你定义它们,它应该工作:

private int RightScore = 0;
private int LeftScore = 0;

void Update()
{
    //boundaries 
    if (Ball.transform.position.x < -5.61f)
    {
        RightScore++;
        print(RightScore);
    }

    if (Ball.transform.position.x > 5.5f)
    {
        LeftScore++;
        print(LeftScore);
    }
            
}

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

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