Search Unity

I have a little problem here

Discussion in 'Scripting' started by karsten-heim, Jun 29, 2022.

  1. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    63
    Ummmm, so I have this code for my tower defense thingy, and when the counter 2 is 1, It should instantiate the specified amount of enemies, but the code will run and when Counter = 2, and the coroutine spawner is running, it will not run the second if statement, but it runs the first if statement just fine before this. do I add break or something in the first if statement or is the entire code just wrong?


    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (timer <= 0f)
    4.         {
    5.            
    6.             timer = wavebarrier;
    7.             spawne();
    8.             //Invoke("spawner", 2.0f);
    9.             //spawner();
    10.  
    11.         }
    12.         timer -= Time.deltaTime;
    13.     }
    14.  
    15.     void spawne()
    16.     {
    17.         if (counter2 <= 0)
    18.         {
    19.             counter = counter + 0.2f;
    20.             Debug.Log("e");
    21.             counter2++;
    22.            // StartCoroutine(spawner());
    23.         }
    24.        /* else if (counter2 == -2)
    25.         {
    26.             counter = counter + 1f;
    27.             Debug.Log("e");
    28.         }
    29.         else if (counter2 == -1)
    30.         {
    31.             counter = counter + 1f;
    32.             Debug.Log("e");
    33.         }
    34.         else if (counter2 == -0)
    35.         {
    36.             counter = counter + 1f;
    37.             Debug.Log("e");
    38.         }
    39.         else if (counter2 == 0)
    40.         {
    41.             counter = counter + 1f;
    42.             Debug.Log("e");
    43.         }*/
    44.         else
    45.         {
    46.             StartCoroutine(spawner());
    47.             counter2 = -4f;
    48.             //wavebarrier = wavebarrier + e;
    49.            
    50.         }
    51.     }
    52.     IEnumerator spawner()
    53.     {
    54.         Debug.Log("wave spawned");
    55.         if (counter == 1)
    56.         {
    57.             for (int i = 0; i <= 2; i++)
    58.             {
    59.                 yield return new WaitForSeconds(1.0f);
    60.                 Instantiate(enemy, spawnpoint.position, spawnpoint.rotation);  // <------ this runs just fine
    61.                 Debug.Log("1");
    62.             }
    63.         }
    64.         if (counter == 2)
    65.         {
    66.             for (int i = 0; i <= 5; i++)
    67.             {
    68.                 yield return new WaitForSeconds(1.0f);
    69.                 Instantiate(enemy, spawnpoint.position, spawnpoint.rotation);   /*   <----- this refuses to run, even when the coroutine is running and the if statement is true. */
    70.                 Debug.Log("2");
    71.             }
    72.         }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    You're comparing equality on floating point values. Due to floating point inaccuracy, it's very unlikely to land exactly on a particular value.

    You probably just want to use >= (greater than or equals to) in your situation.
     
  3. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    63
    I am not entirely sure what you mean by floating point inaccuracy, but I can assure you that the float "counter" is equal to the value in the if statement
     
  4. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    63
    I am not entirely sure what you mean by floating point inaccuracy, but I can assure you that the float "counter" is equal to the value in the if statement
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    By your own admission you don't know what you're talking about. Here let me enlighten you on the fun adventure of floating point arithmetic:
    upload_2022-6-29_9-11-58.png
    Of exceptional interest here is the fact that 0.2 is not a number that can be cleanly represented in binary (much like 1/3 cannot be represented in base 10):
    upload_2022-6-29_9-14-4.png
     
    spiney199 and Kurt-Dekker like this.
  7. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    63
    so what floats would be considered accurate in a base 2? I could also just use integers right?
     
  8. karsten-heim

    karsten-heim

    Joined:
    May 16, 2022
    Posts:
    63
    or could I use a range such as

    if(counter <= 1.9 && counter >=2.1)

    or would the slightly skewed value just continue to go up or down very slowly until the range is no longer accurate?
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    you could and should just use integers, yes. There's no good reason to use floats here.
     
  10. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    A value cannot be both less than/equal to 1.9 AND greater than/equal to 2.1 . Just use integers.
     
  11. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,148
    None of them. Floating point numbers in modern devices follow the IEEE-754 standard which is designed to make it easier to process and store real numbers at the cost of accuracy. Fixed point numbers have the accuracy but they lose the high performance and low memory consumption benefits of floating point numbers.

    There are a number of libraries available to let you use fixed point numbers. Here is one for Unity.

    https://github.com/danielmansson/Unity.Mathematics.FixedPoint
     
    Last edited: Jul 6, 2022