- Home /
Why can't I get a floating point value here?...
Possibly the stupidest question I've asked here yet, but, I seem only able to get the integer part back from what I'm pretty sure is a float calculation!. I'm having flashbacks to FORTRAN and implicitly typed variable names...;^) I've tried a bunch of crazy stuff to get a float as you can see in the included code segment but the Debug.Log definitely only shows integer values, (which really messes up mesh alignments!).
Bathys = new GameObject[x_meshes,z_meshes];
float fi = 0;
float fj = 0;
for (int i=0; i<x_meshes; i++) {
for (int j=0; j<z_meshes; j++){
// Generate named gameobjects and make them children of bathyBoss object.
// It isn't strictly necessary to provide names for the GameObjects
// generated by the script, but, they show up in the hierarcy at run
// time so they look cleaner, and being able to find by name helps in
// clean up for regenerations and such.
string goname = System.String.Format("Bathy_{0}{1}",i,j);
Bathys[i,j] = new GameObject(goname);
Bathys[i,j].transform.parent = bathyBoss.transform;
Bathys[i,j].AddComponent<MeshRenderer>();
Bathys[i,j].AddComponent<MeshFilter>();
Bathys[i,j].AddComponent<ShowMeshVertices>();
Mesh bathy = new Mesh();
bathy.vertices = GenVertices ();
Bathys[i,j].GetComponent<MeshFilter>().sharedMesh = bathy;
// add a mesh collider for flythrough terrain avoidance
MeshCollider mshcol = Bathys[i,j].AddComponent(typeof(MeshCollider)) as MeshCollider;
mshcol.sharedMesh = bathy;
// position mesh's gameobject for correct overlap
/*
ij
10 | 11
---+---
00 | 01
*/
// float x_off = ((float)i)*x_cells;
// float z_off = ((float)j)*z_cells;
float x_off = fi*(float)x_cells; // need some work for other than 1.0 sizes
float z_off = fj*(float)z_cells;
// WHAT IN THE...?!! CAN'T MAKE A FLOAT HERE...
Debug.Log("x_off " + i + " = " + x_off);
Debug.Log("z_off " + j + " = " + z_off);
Bathys[i,j].transform.position = new Vector3((float)x_off,0f,(float)z_off);
fj++;
}
fi++;
fj=0;
}
}
I don't totally understand the question. I see that your adding one to fj, and fi, and your multiplying them by x_cells and z_cells... what is supposed to happen? Whats the expected outcome? What happens? What should the information that debug log contains show you?
Just looking at it, seems to me your multiplying a whole number, by a whole number (float or not) and the result is... a whole number? What is the problem though? If you had typed "Debug.Log(x_off + 0.25487f);" you will end up with with a non-whole number...
Another thing is, you don't need to cast a float as a float (like "(float)x_off" ) because it is already a float.
Hi $$anonymous$$D_Reptile, I'm aware you shouldn't have to cast x_off to a float, but, I've been trying everything I can do to get a float result...;^) The problem is I get integer results here that cause issues elsewhere in the code when they get used and need to be floats so other results don't become ints.
I agree with @$$anonymous$$D_reptile - I can't see the problem here. You know that by the time you're seeing the result of Debug.Log, it's not a float or an int? It's a string.... are you just being puzzled by the way it is displayed in the console window?
What value are x_cells and z_cells? If they're whole numbers, I see no issue here. What were you expecting to print? What actually printed?
Answer by Outliver · May 02, 2016 at 06:26 PM
Try to set
float fi = 0f;
instead of
float fi = 0;
And try
fi += 1.0f;
instead of
fi++
Not sure but that could fix it.