I’m new to Unity and what I want to do is use a vector2 as an index position for a 2D Array, like this
2dArray[vector2] = int;
I’ve tried a lot of ways to get this to work but haven’t found any. Any ideas would help.
I’m new to Unity and what I want to do is use a vector2 as an index position for a 2D Array, like this
2dArray[vector2] = int;
I’ve tried a lot of ways to get this to work but haven’t found any. Any ideas would help.
A two-dimensional array has this format:
int[,] myArray
and elements are accessed like this:
myArray[3,76] = myValue;
So much for the basics. To use a Vector2, you first have to be aware of the fact that a Vector2 consists of 2 floats, and not integers. That’s ok though, as long as the floats always are whole numbers. Otherwise, you will have to round, or floor, them to an integer.
Vector2 myVector2 = new Vector2(3,76);
myArray[myVector2.x,myVector2.y] = myValue;
As mentioned above, it’s possible that you have to cast the floats as integers, or round them, in order to be accepted as indices for the two-dimensional array.
See also: