Problem With float

Hi all… so in the script im using here… im getting the function “SpawnFaster” to decrease the value of spawnWait by .1 but if spawn wait = .1 then it will not decrease it… so this was all going fine and i have a debug.log that tells me the value of it… and at one point i got this very strange result :stuck_out_tongue: and im not sure why… It might have to do with the fact that i had to change .1 to .1f? im not sure :stuck_out_tongue: Please help!!

also the value of spawnWait starts at 5

using UnityEngine;
using System.Collections;

public class Spawn : MonoBehaviour 
{
	public GameObject enemy;
	public Vector3 spawnValues;
	public int enemyCount;
	public float spawnWait;
	public float startWait;
	public float ySpawnValue;
	
	
	void Start ()
	{
		StartCoroutine (SpawnWaves ());
		spawnWait = 5;
	}
	
	public void SpawnFaster ()
	{
		if (spawnWait > .1) 
		{
			spawnWait -= .1f;
		}
		Debug.Log (spawnWait);
	}

52324-screen-shot-2015-08-18-at-101337-pm.png

It is actually normal.
See “What Every Computer Scientist Should Know About Floating-Point Arithmetic” : What Every Computer Scientist Should Know About Floating-Point Arithmetic

I guess this doesn’t cause any problem does it?

yes i understand… sorry about that :frowning: @Bunny83 i was currently trying to figure it out and did not give a good answer… but i did figure it out… i converted the variable spawnWait into a decimal… and when needed turned it into a float… i didn’t post the whole code earlier… but here it is:

using
UnityEngine;
using System.Collections;

public class Spawn : MonoBehaviour
{
public GameObject enemy;
public Vector3 spawnValues;
public int enemyCount;
public decimal spawnWait;
public float startWait;
public float ySpawnValue;

void Start ()
{
	StartCoroutine (SpawnWaves ());
	//starts the funcion Spawnwaves

	spawnWait = 5;
}

public void SpawnFaster ()
{
	if (spawnWait > .1M) 
	//if the spawn wait is greater than the selected number.. it will not be triggered anymore
	{
		spawnWait = spawnWait - 0.1M;
		//spawnWait is decreased by the selected number
	}
	//Debug.Log (spawnWait);
}

IEnumerator SpawnWaves ()
{
	yield return new WaitForSeconds (startWait);
	while (true)
	{
		
		for(int i = 0; i < enemyCount; i++)
		//since i = 0 and enemyCount will always be greater than it.. if i becomes greater than enemyCount.. the loop stops
		{
			Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 1, Random.Range (-spawnValues.z, spawnValues.z));
			//grabs a random range on the axis' of z and x

			Quaternion spawnRotation = Quaternion.identity;
			//not sure what the hell this does.. but code!!!

			Instantiate (enemy, spawnPosition + transform.TransformPoint(0, 0, 0), spawnRotation);
			//brings the enmy in at the spawnposition

			yield return new WaitForSeconds ((float)spawnWait);
		}
	}
}

}