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);

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.


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<>

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.