My if function doesn't do what I want it to do

So, the second if function is supposed to activate other screens in the game, as soon as it reaches the final amount of a jackpot. IT reaches and the timer stops as soon as it does, but my second if function is not doing what is supposed to do. So I took it out, and it works, but it doesn’t even allow me to fill in the Input fields for the jackpot to start increasing. Can someone please help me?

	if (timerRunning1) {
		timerValue1 += Time.deltaTime * 14.4 * (timerValue1F - timerValue1) / (finish1 - start).TotalSeconds;
		Jack1.text = timerValue1.ToString ("F2");

		//PlayerPrefs.SetFloat ("Jackpot1", float.Parse(Jackpot1.text));
		//PlayerPrefs.SetFloat ("Jackpot1f", float.Parse(Jackpot1F.text));
	

		if (timerValue1 == timerValue1F) {

			print ("ALTO");
			timerRunning1 = false;

			PagPrincipal.SetActive (false);

			Victoria.SetActive (true);

			Debug.Log ("FUNCIONA!!!");
		
			Invoke ("apagavictoria", 10f);

		
		
		}	

	}

This question's title is the best worst title I've seen in a long time XD

3 Answers

3

I can not see the declaration of your attributes or variables, but I guess your variables timerValue1 and timerValue1F are floats. That both variables get the same value is difficult due to the precision. Please use a relation operator in your if-clause, like >= or <= instead of the equal operator ==.

I hope this answer is helpful for you. For a more specify answer I need more detailed information.

As @MASTware pointed out, because of the lack of precision using floats, NEVER, NEVER try to compare floating values using the == comparison symbol (especially when you use values related to time ( Time.deltaTime )). Instead, use a range comparison using Mathf.Abs for example :

 if ( timerValue1 >= timerValue1F - 0.01f && timerValue1 <= timerValue1F + 0.01f )

----- OR

 if ( Mathf.Abs( timerValue1 - timerValue1F ) <= 0.01f )

if (timerValue1 > timerValue1F)

No, I made it on mac