Making a ranking system but is not working

Im trying to make a ranking system for my game and i am having a issue in the code, there is rank S, A, B and F but the code never goes bellow the A rank for some reason

using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class ScoreManager : MonoBehaviour
{
    [Header("Tempo")]
    public TextMeshProUGUI timerText;
    public float _time;

    [Header("Rank da Fase")]
    public float timeToFinish;


    private void Update()
    {
        //AddScore();
        Timer();
    }

    private void Timer()
    {
        _time = Time.fixedTime;
        TimeSpan time = TimeSpan.FromSeconds(_time);
        timerText.text = "Tempo: " + time.Minutes.ToString() + ":" + time.Seconds.ToString();
    }

    public void TimeToFinish()
    {
        if (_time <= timeToFinish)
        {
            Debug.Log("Rank S");
        }

        else if (_time >= timeToFinish)
        {
            Debug.Log("Rank A");
        }

        else if (_time >= timeToFinish * 0.3f)
        {
            Debug.Log("Rank B");
        }

        else if (_time >= timeToFinish * 0.4f)
        {
            Debug.Log("Rank F");
        }
    }
}

Hopefully someone can help

2 Answers

2

You need to invert the else if. Think about it if your time is bigger than the expected it will always enter (your time: 20 > expected: 10). So u need to check not how good but more how bad it was so i recommend starting from below.

_time >= worst time (time to * 0.4)
_time >= slightly better time (time to * 0.3)
_time >= good time (time)

Another way would be to check a delta: _time/timeToFinish < 1 … > 1.4 … > 1.3 … > 1

Can you explain your intent for the scoring system? Your current statements seem inconsistent.
Both the B and F rank seem to be looking for periods of time that could be less than timeToFinish, but A rank is awarded if the time is greater than timeToFinish.

To directly answer your question, the reason you only see S and A rank is because your first two if statements entirely encompass all possibilities, so the if else chain never goes any further. _time is either less than (or equal) timeToFinish or it is greater than timeToFinish.

Maybe you wanted something like this:

if (_time <= timeToFinish)
{
   Debug.Log("Rank S");
}
else if (_time <= 1.3f * timeToFinish) // only reach here if _time > timeToFinish
{
    Debug.Log("Rank A");
}
else if((_time <= 1.4 * timeToFinish) //only reach here if _time > 1.3 * timeToFinish
{
    Debug.Log("Rank B");
}
else  //only reach here if _time > 1.4 * timeToFinish
{
    Debug.Log("Rank F");
}

This is exactly what i was looking for thank you very much, now i understood what i was doing wrong