- Home /
 
zero is not zero
Hey, i have a problem with my code. i am trying to make an 2D Array of gameobjects. It works good so far. The only problem: In the Editor the Y value of the objects that are at 0 is not 0 but -9.536743e-07. In the scene window everything looks perfect and it does look like it is 0. But it does not show the 0 in the inspector.
Any ideas?
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DungeonManager : MonoBehaviour
 {
     public int dungeonGridSize = 7;
     public float dungeonTileSizeX = 19.2f;
     public float dungeonTileSizeY = 10.8f;
     private float generationStartPosX, generationStartPosY;
     private float currentGenPosX, currentGenPosY;
     public GameObject[,] dungeon;
     public GameObject emptyRoom;
 
     // Start is called before the first frame update
     void Start()
     {
         SetGenerationStartPos();
         GenerateEmptyDungeon();
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     void GenerateEmptyDungeon()
     {
         dungeon = new GameObject[dungeonGridSize, dungeonGridSize];
         for(int i = 0; i < dungeonGridSize; i++)
         {
             for (int j = 0; j < dungeonGridSize; j++)
             {
                currentGenPosX = generationStartPosX + (i * dungeonTileSizeX);
                currentGenPosY = generationStartPosY + (j * dungeonTileSizeY);
                 print(currentGenPosX);
                 print(currentGenPosY);
                dungeon[i,j] = Instantiate(emptyRoom, new Vector2(currentGenPosX,currentGenPosY), Quaternion.identity);
             }
         }
     }
 
     void SetGenerationStartPos()
     {
         generationStartPosX = (((dungeonGridSize - 1) / 2)*dungeonTileSizeX)*(-1);
         generationStartPosY = (((dungeonGridSize - 1) / 2)*dungeonTileSizeY)*(-1);
 
     }
 }
 
 
              Answer by Nexoan · Dec 11, 2020 at 06:05 PM
Hello! If your object is parented, then it is measured relative to the objects parent.
Answer by CmdrZin · Dec 12, 2020 at 07:17 AM
Since its a calculated float, 0.0000009 instead of 0.0000000 is probably a precision error but can be considered 0. As a side note, you never use X == 0.0f with floats because they rarely equal exactly 0.0000000.
Your answer
 
             Follow this Question
Related Questions
How can I get correct figure from formula of float 1 Answer
How to get float value from other script? 2 Answers
Multiple Cars not working 1 Answer
C# round transform.position floats to .5 2 Answers
Have a problems with a values 1 Answer