Search Unity

Question My if-statement is not being read

Discussion in 'Scripting' started by Bl00dyFish, Feb 21, 2024.

  1. Bl00dyFish

    Bl00dyFish

    Joined:
    Mar 12, 2022
    Posts:
    84
    Hello.
    I'm working on a script to generate platforms infinitely. I got it started but ran into an issue when I checked for the instantiated platform's position.

    First off, here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.VisualScripting;
    4. using UnityEngine;
    5.  
    6. public class Generation : MonoBehaviour
    7. {
    8.     [SerializeField] GameObject[] GroundLevels;
    9.     [SerializeField] GameObject startPlatform;
    10.     [SerializeField] GameObject lastInstantiatedPlatform;
    11.    
    12.     public float generationSpeed;
    13.    
    14.  
    15.     private List<GameObject> instantiatedLevels;
    16.  
    17.  
    18.     void Start()
    19.     {
    20.         instantiatedLevels = new List<GameObject>();
    21.        
    22.         //Create the starting platform (The first platform that will be instantaited
    23.         lastInstantiatedPlatform = Instantiate(startPlatform, transform.position, Quaternion.identity);
    24.         instantiatedLevels.Add(lastInstantiatedPlatform);
    25.  
    26.     }
    27.  
    28.  
    29.     void Update()
    30.     {
    31.  
    32.         foreach (GameObject level in instantiatedLevels)
    33.         {
    34.             level.transform.Translate(Vector3.back * generationSpeed);
    35.         }
    36.  
    37.  
    38.         if (lastInstantiatedPlatform.transform.position.z == -5)
    39.         {
    40.             print("next!");
    41.         }
    42.         print(lastInstantiatedPlatform.transform.position.z);
    43.     }
    44.  
    45.     GameObject CreateNewPlatform()
    46.     {
    47.         GameObject nextPlatform = Instantiate(GroundLevels[Random.Range(0, GroundLevels.Length)], transform.position, Quaternion.identity);
    48.         instantiatedLevels.Add(nextPlatform);
    49.         return nextPlatform;
    50.        
    51.     }
    52. }
    I wanted to spawn the platforms when the lastInstantiatedPlatform.transform.position.z was equal to its negative scale in the z-axis. However, when this didn't work, I just changed it to print "next!" when its position.z equaled -5.

    When this didn't work, I inserted the
    Code (CSharp):
    1. print(lastInstantiatedPlatform.transform.position.z);
    to make sure that I was actually getting the z position. And I was! -5 was printed, and "next!" was not printed.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,813
    You're running into floating point precision issues. A floating point number is very rarely ever going to be exactly equal to a whole number. You probably should just check if it's greater than or less than a value, depending on what you're doing.

    Also how hard is it to post scripting questions in the scripting subforum?
     
    Bunny83 likes this.
  3. Bl00dyFish

    Bl00dyFish

    Joined:
    Mar 12, 2022
    Posts:
    84
    Thank you, this works
     
  4. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497