0
\$\begingroup\$

The counter counts perfectly up until the first bump. After this it starts to add about 4-5 "points" instead of just 1.

using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour
{

    public GUIText scoreText;
    public int score;

// CONVERT FLOAT TO INT?


    void Start (){
    score = 0;
    StartCoroutine (FeetScoreCounter());
    }

    // STARTS COUNTING AFTER WAITFORSECONDS
    IEnumerator FeetScoreCounter() {
    yield return new WaitForSeconds (1);
    InvokeRepeating("scoreCounter", 2, 1);
}

// FOR EACH 5 SECONDS; score++ ???

    void Update () {
    scoreText.text = score + " ft";
 //     StartCoroutine (FeetScoreCounter());

}

void scoreCounter () {
    score += 1;
    StartCoroutine (FeetScoreCounter());
    score ++;
}

}

I want my counter to start counting when the game starts, to show how far up the balloon is. I am incapable of seeing why my code just randomly adds a whole lot more over time. Care to help?

\$\endgroup\$
4
  • \$\begingroup\$ It adds a whole lot of numbers because your logic is crazy! If your character moves down the screen at the same pace every single time then why not just set your score to "(int)Time.time" which is the amount of time in seconds that the scene has been running for? \$\endgroup\$
    – Savlon
    Commented Nov 20, 2014 at 10:43
  • \$\begingroup\$ A combination of me being new to programming, tired and classmates playing rave-music in 100 decibels does disturb my thinking for quite a bit. Thanks for the answer, I will look into it. :) \$\endgroup\$
    – Blobbey
    Commented Nov 20, 2014 at 10:45
  • \$\begingroup\$ We all have to start somewhere! :) Good luck \$\endgroup\$
    – Savlon
    Commented Nov 20, 2014 at 10:46
  • \$\begingroup\$ void Start (){score = Time.time;} void Update () {scoreText.text = score + " ft";} It feels like something along those lines are correct. It might be better if I just supress my excitement for a couple hours until I get home. \$\endgroup\$
    – Blobbey
    Commented Nov 20, 2014 at 10:50

2 Answers 2

1
\$\begingroup\$

It looks like you have your score working using Time.time as the counter.

I thought it may be helpful to explain what was happening when the code in your question is executed.

  1. When game starts and the Start() method runs it calls StartCoroutine (FeetScoreCounter())
  2. When FeetScoreCounter() runs it immediately yields for one second, then after that delay it calls InvokeRepeating("scoreCounter", 2, 1). This will cause scoreCounter() to run repeatedly starting after a two second delay, and then again every second.
  3. When scoreCounter() runs it: A) increments score, B) calls FeetScoreCounter() (see step 2) , and then C) increments score again.

The first time InvokeRepeating("scoreCounter", 2, 1) is executed it causes scorecounter() to run in a repeating manner: wait couple of seconds then once per second until canceled.

Each subsequent call of InvokeRepeating("scoreCounter", 2, 1) has the same effect, but the earlier ones are never canceled.

Each time scoreCounter() runs it calls FeetScoreCounter() (and InvokeRepeating()) triggering an additional call of scoreCounter() to occur each second from then on.

As a result you quickly have many parallel InvokeRepeating() requests each calling scoreCounter() on a regular schedule. This will cause the score to increase at an exponential rate.


An alternative implementation might look like this:

public class GameController : MonoBehaviour {

    public GUIText scoreText;
    public int score;
    public int delay = 2;
    public int frequency = 1;

    void Start () {
        score = 0;
        InvokeRepeating("scoreCounter", delay, frequency);
    }

    void scoreCounter () {
        score++;
        scoreText.text = score + " ft";
    }

}
\$\endgroup\$
0
\$\begingroup\$

enter image description here

This was the correct solution, where it converts score (float) to an integer.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .