Search Unity

Need help with my scorer

Discussion in 'Getting Started' started by Iqew, Sep 24, 2017.

  1. Iqew

    Iqew

    Joined:
    Nov 26, 2016
    Posts:
    38
    So at the moment, I'm making a small little mobile game and I have a scorer working. What I want to do is to make the scorer accelerate (score faster) when the numerical value of score has reached a certain threshold. I used a vector3 to represent the 3 thresholds at which point the scorer will score faster. The problem is, whenever the score has reached threshold#1, it stops counting. Can someone please evaluate my script?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class TextController : MonoBehaviour {
    7.  
    8.     public GameObject Player;
    9.     public Vector3 ScoreTargets;
    10.     private float Score;
    11.     public Text Self;
    12.  
    13.     IEnumerator Scorer(){
    14.         while(Score < ScoreTargets.x){
    15.             Score += 25;
    16.             Debug.Log(Score);
    17.             yield return new WaitForSeconds(.1f);
    18.             }
    19.         while(Score < ScoreTargets.y && Score > ScoreTargets.x){
    20.             Score += 100;
    21.             Debug.Log(Score);
    22.             yield return new WaitForSeconds(.1f);
    23.             }
    24.         while(Score < ScoreTargets.z && Score < ScoreTargets.y && Score < ScoreTargets.x){
    25.             Score += 150;
    26.             Debug.Log(Score);
    27.             yield return new WaitForSeconds(.1f);
    28.             }
    29.    
    30.         yield return new WaitForSeconds(1);
    31.     }
    32.  
    33.     void Start () {
    34.         StartCoroutine("Scorer");
    35.     }
    36.    
    37.     // Update is called once per frame
    38.     void Update () {
    39.         Self.text = "Score: " + Score;
    40.     }
    41. }
    42.  
    here's a little video of what's happening. Just skip near the end to see the counter stop
     
  2. mahewitt

    mahewitt

    Joined:
    Mar 19, 2014
    Posts:
    279
    Hi.

    You could try adding Debug.Log statements or using breakpoints to see clearer what is happening.

    One cause though might be line 19 that should perhaps read (>=):

    while(Score < ScoreTargets.y && Score >= ScoreTargets.x){

    if Score equals ScoreTargets.x i.e. 1000 to exit the first condition then the second while loop would never run as Score is not greater than 1000, only equal.

    Regards,
    Mark

    -----
    Check out my assets: Game Framework (Free), Beautiful Transitions and more...