简体   繁体   English

C#得分计数器{Unity3D}

[英]C# Score Counter {Unity3D}

I need to make a score counter for my game. 我需要为我的游戏计分。 It partly works, whenever the cube reaches (x)11 the score goes up, and the block will be reset to it's beginning position at (x)12. 每当多维数据集达到(x)11时分数就会上升,并且该块将被重置为其在(x)12处的开始位置,这部分起作用。 The problem is, is that whenever the cube reaches (x)11 it will keep counting +1 until (x)12, when it gets reset the score is around 36 most of the time. 问题是,每当多维数据集到达(x)11时,它将一直计数+1直到(x)12,当它重置时,分数通常在36左右。 It keeps doing this and I have no clue what I have done wrong, full on noobie here. 它一直在这样做,我不知道我做错了什么,这里有很多关于noobie的信息。 Here is my code: 这是我的代码:

void Start () {
    beginPositie = new Vector3 (-4, 0, 0);
    transform.position = beginPositie;
    score = 0;
}


void Update () {
    snelheid = Random.Range (2, 5);
    transform.Translate (snelheid * Time.deltaTime, 0, 0);
    if(gameObject.transform.position.x>= 12){
        transform.position = beginPositie;
    }

}

void OnGUI() {
    if (gameObject.transform.position.x == 11) {
        score = score + 1;
    }
    GUI.skin = textSkin;
    GUI.Label (new Rect (10, 10, 300, 100), "Score: " + score);

}

You have two options: you can store in a boolean if you have increased the score this cycle and reset it on reseting the cube position: 您有两个选择:如果您在此循环中提高了分数,则可以将其存储在布尔值中,并在重置多维数据集位置时将其重置:

bool countedThisCycle; // What you use to determine if you have scored this cycle.
void Start () {
    beginPositie = new Vector3 (-4, 0, 0);
    transform.position = beginPositie;
    score = 0;
    countedThisCycle = false; // Make sure you count the first one.
}


void Update () {
    snelheid = Random.Range (2, 5);
    transform.Translate (snelheid * Time.deltaTime, 0, 0);
    if(gameObject.transform.position.x>= 12){
        transform.position = beginPositie;
        countedThisCycle = false; // Make sure you can count the next one.
    }
}

void OnGUI() {
    if (gameObject.transform.position.x == 11 && !countedThisCycle) { // Check that you haven't counted this one.
        score = score + 1;
        countedThisCycle = true; // Make sure you don't count it again.
    }
    GUI.skin = textSkin;
    GUI.Label (new Rect (10, 10, 300, 100), "Score: " + score);
}

Your other option which programatically is more correct, is to count when you do the reset, but that probably beats the purpose of counting when you reach 11. 从编程上来说更正确的另一种选择是在进行重置时计数,但是当达到11时,这可能会超过计数的目的。

This is causing the problem: 这导致了问题:

if (gameObject.transform.position.x == 11)

Do not compare floats with = because it is never guaranteed that the value of gameObject.transform.position.x will exactly be 11 . 不要将浮点数与=进行比较,因为永远不能保证gameObject.transform.position.x的值将恰好为11

That should b changed to: 应将b更改为:

if (gameObject.transform.position.x >= 11)

Another unrelated problem is OnGUI() . 另一个不相关的问题是OnGUI() Don't use it. 不要使用它。 Use the new Unity UI . 使用新的Unity UI

Even though you already fixed your problem I think I can help you understand what went wrong. 即使您已经解决了问题,我想我也可以帮助您了解问题所在。

We dont know how often update() and OnGUI() are called. 我们不知道update()OnGUI()的调用频率。 It is very plausable that OnGUI() is called more often then Update() . 似乎比Update()更频繁地调用OnGUI()是很合理的。 Which would make the call stack look something like this: 这会使调用堆栈看起来像这样:

Update();
OnGUI();
OnGUI();
OnGUI();
OnGUI();
OnGUI();
Update();
OnGUI();
OnGUI();
OnGUI();
OnGUI();
OnGUI();

Now if your Update() recalculates and evaluates position to 11, your OnGUI will see 11 and update your score. 现在,如果您的Update()重新计算并将位置评估为11,则OnGUI将看到11并更新您的得分。 Since OnGUI is called more often it will keep seeing that 11. 5 times in my example, resulting in your score being raised 5 times in a row, before the next Update() happens. 由于OnGUI的调用频率更高,因此在我的示例中将不断看到11。5倍,导致您的分数在下一次Update()发生之前连续提高5倍。

My advice would be to keep everything game logic related to game logic methods and everything UI related in UI related methods. 我的建议是保持与游戏逻辑方法相关的所有游戏逻辑,以及与UI相关的方法中相关的所有UI。 Avoid mixing them! 避免混合它们!

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

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