Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rosscoe3 · Aug 19, 2015 at 02:17 AM · floatif-statementsvalue

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 :P and im not sure why.. It might have to do with the fact that i had to change .1 to .1f? im not sure :P 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);
     }

alt text

screen-shot-2015-08-18-at-101337-pm.png (25.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by GiyomuGames · Aug 19, 2015 at 02:33 AM

It is actually normal. See "What Every Computer Scientist Should Know About Floating-Point Arithmetic" : http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rosscoe3 · Aug 20, 2015 at 12:03 AM 0
Share

mm it does :P

avatar image GiyomuGames · Aug 20, 2015 at 12:07 AM 0
Share

If it's a problem then you can use int ins$$anonymous$$d. Start from 50 and remove 1 by 1.

avatar image Eno-Khaon · Aug 20, 2015 at 12:13 AM 2
Share

On a related note, floating point imprecision is the key reason Unity provides the Appoximately operator.

When you need to compare whether two values are the same, floating point numbers have to be "close enough" ins$$anonymous$$d of identical to be reliable. If you just need to compare greater/less than, however, < and > are generally going to be reliable enough, since you don't need to worry *quite* as much.

avatar image Bunny83 · Aug 20, 2015 at 12:55 AM 1
Share

@Rosscoe3:
"It does" isn't a good explanation why it's a problem... What exactly doesn't work the way you want / expected it to behave? If you don't put some more efford into presenting your actual problem we'll just close this question since it doesn't contain any concrete problem.

avatar image
0

Answer by Rosscoe3 · Aug 20, 2015 at 01:30 AM

yes i understand.. sorry about that :( @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);
         }
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing float from dictionary 2 Answers

Problems with pulling a slider value - (Making sensitivity Sliders) 0 Answers

Change value according to Y position of an object? 2 Answers

Questions about a random value between two numbers 2 Answers

Getting a slider value from a different scene 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges