Display calculations

Hi all,

I am trying to display the “Rating calculation” but it is showing “NaN” on screen.

And how do I use the new UI system to display my calculations

Code is as follows:

	public float _timer;
	public float _miss;
	public float _hit;
	public float _rating;

	// Use this for initialization
	void Start () {
	
		RatingCalculation ();

	}

	void OnGUI()
	{
		_timer = PlayerPrefs.GetFloat("timeTaken");
		Debug.Log (_timer);
		GUILayout.Label("Your time: " + _timer);

		_miss = PlayerPrefs.GetFloat("miss counter");
		Debug.Log (_miss);
		GUILayout.Label("Miss shots: " + _miss);

		_hit = PlayerPrefs.GetFloat("hit counter");
		Debug.Log (_hit);
		GUILayout.Label("On target: " + _hit);


		GUILayout.Label("Your rating is: " + _rating.ToString());




	}

	void RatingCalculation() 
	{
		_rating = _timer /_hit * _miss;


	}
}

NaN means that you’re dividing by zero.

This line:

_rating = _timer / _hit * _miss;

is evaluated like this:

_rating = (_timer / _hit) * _miss;

if _hit is zero, then _timer / _hit can’t be evaluated, and you get NaN (Not a Numer).

That is probably not the rating formula you want. If you want hits to count positive, and misses to count negative, you probably want something along the lines of:

_rating = (_hit - _miss) / _timer;

Anyways, don’t divide by zero.