- Home /
Monodevelop is expecting int from my float array
Unity is giving me an error in regards to my float array. Its expecting int even though i set the array as float. There is an image i uploaded of the error. Thank You
public class SphereChunk : MonoBehaviour {
public float[,,] map;
public Mesh visualMesh;
protected MeshRenderer meshRenderer;
protected MeshCollider meshCollider;
protected MeshFilter meshFilter;
// Use this for initialization
void Start () {
meshRenderer = GetComponent<MeshRenderer> ();
meshCollider = GetComponent<MeshCollider> ();
meshFilter = GetComponent<MeshFilter> ();
float width = 10/SphereWorld.currentSphereWorld.radius;
map = new float[0.1f, 10f, 0.1f];
for (float x = 0; x < width; x+= SphereWorld.currentSphereWorld.polar) {
for (float y = SphereWorld.currentSphereWorld.radius; y < World.currentWorld.chunkHeight + SphereWorld.currentSphereWorld.radius; y++) {
for (float z = 0; z < width; z+= World.currentWorld.chunkWidth * SphereWorld.currentSphereWorld.polar) {
float a = y * Mathf.Cos(SphereWorld.currentSphereWorld.elev);
x = a * Mathf.Cos(SphereWorld.currentSphereWorld.polar);
y = y * Mathf.Cos(SphereWorld.currentSphereWorld.elev);
z = a * Mathf.Cos(SphereWorld.currentSphereWorld.polar);
map[x, y, z] = 1;
Debug.Log("X is : " + x + " Y is : " + y + " Z is : " + z);
}
}
}
}
You have declared multidimensional array and giving float values to array. This will help you understand how to declare and use arrays http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
yea, i've realized that i want my indices to be floats and indices must be ints. So here is my dilemma. I want to give the array a value at indices x,y,z which is a type of brick. However x,y,z need to be floats. Any suggestions?
Answer by wibble82 · Jan 31, 2014 at 01:00 PM
Hi There
I think you've misunderstood the functionlity of the float[,,] variable a little.
You would probably recognise the standard definition of an array:
float[] myfloatarray;
This is defining a list of values, each of which is a float. When setting it up you might say:
myfloatarray = new float[10]
Meaning 'I want a list of 10 floats'.
You then get each of those floats by using an 'index' which is always an integer. So you can say 'give me the float at index 4 (i.e. the 5th float)':
afloat = myfloatarray[4]
But there's no such thing as myfloatarray[0.1]!
The code you're using simply defines a 'multi dimensional array', so:
float[,] myfloatgrid = new float[10,10]
is a 10x10 2d grid of numbers.
float[,,] myfloatgrid = new float[10,10,10]
is a 10x10x10 3d grid of numbers.
As before though, you always index it with a integers. In the 2d case its indexed with 2 integers, and in the 3d case its 3 integers.
Without knowing the exact thing you're trying to do I can't quite offer a solution, however that's the root of the errors you're seeing.
-Chris
Answer by james_170482 · Jan 31, 2014 at 04:16 PM
I think the issue is that you have not set a size for your array.
Unless you know exactly how big the array will need to be your best of using List<>
http://answers.unity3d.com/questions/531953/creating-a-multidimensional-array-list.html
Answer by KellyThomas · Feb 01, 2014 at 01:28 AM
You can convert from float
to int
by several techniques.
Each of these has different behaviours:
float myFloat = 4.5f;
int x;
x = (int)myFloat; // casting will round toward zero
x = Mathf.FloorToInt(myFloat); // floor will round to the low value
x = Mathf.RoundToInt(myFloat); // round will round to the closest value
x = Mathf.CeilToInt(myFloat); // ceiling will round to the high value
You will need to decide which is appropriate for your circumstances.
Your answer
Follow this Question
Related Questions
Have a problems with a values 1 Answer
Can I create a list with an int/float and a string? C# 2 Answers
Floats and int performance difference? 2 Answers
Convert a char to int / float 2 Answers