Float surprisingly turns to int or 0
Hi Guys, (first post here)
i would be happy if someone could check my code. For me it seems a float variable "surprisingly" turns into 0 (maybe an integer). Watch the variable offsetx. in the GenerateLevel function it is 0.7. Later, in the GenerateTiles function it turns to 0. In my opinion without any operation with an integer involved.
private Level levelData;
private int levelnr = 1;
public float offsetx;
private float offsety;
public Transform Wall;
public void GenerateLevel()
{
levelData = new Level();
levelData.LoadLevel(levelnr);
GenerateTiles(levelData.height, levelData.width);
offsetx = (float)levelData.tileSizeWidth/100f;
offsety = (float)levelData.tileSizeHeight/100f;
Debug.Log(levelData.tileSizeWidth);
Debug.Log("GNA: " + offsetx); <--- gives me a "0.7"
}
public void GenerateTiles (int height, int width)
{
for (int i=0; i<height;i++)
{
for (int y=0;y<width;y++)
{
Debug.Log ("FU " + offsetx); <---- gives me "0"
Vector2 position = new Vector2((float)y*offsetx, (float)-i*offsety);
Debug.Log ("U2 " + offsetx);<---- gives me "0"
Debug.Log (position); <------- gives me "(0.0, 0.0)
switch (levelData.data2[i,y])
{
case 9:
Instantiate(Wall, position, Quaternion.identity);
break;
}
}
}
}
void Start ()
{
GenerateLevel();
}
}
Thanks for you help and kind regards.
Answer by Dave-Carlile · Nov 02, 2015 at 01:21 PM
You're calling GenerateTiles before setting offsetx
.
GenerateTiles(levelData.height, levelData.width); // this function will have offsetx as 0
offsetx = (float)levelData.tileSizeWidth/100f;
Hi Dave,
thank you so much. I really wonder how blind one can get to the simplest things. :-) $$anonymous$$E <- stupid&blind.
Love, Peace &Wisdom, $$anonymous$$I$$anonymous$$
Haha, no worries. It's easy to miss things sometimes.
And @Statement, thanks for the s$$anonymous$$lth edit to add some clarity.