- Home /
Access x value in list of Vector3
I've made a list of all possible x/y coordinates with my map area:
for(int x = 0; x <= worldWidth; x++)
{
for(int y = 0; y <= worldHeight; y++)
{
//makes a list of all possible positions for tiles
gridPositions.Add(new Vector3(x, y, 0f));
}
}
I've figured how to render a tile at every position, but next I need to apply a dozen more restrictions to create a landscape. To do this I need to access the x and y values of entries in my list, and I know in javascript and it would be something like gridPosition[i].x
, but I'm unsure how to get it in Unity. Thanks!
I hadn't, but now that I've actually learned a bit more about C# I realize a lot of it is identical to Javascript, which will save me a lot of reading from here out.
Answer by _alphaBeta_ · Jun 05, 2015 at 04:40 PM
From your example, gridPositions would appear to be of type Vector3, so you should be able to do as you suggested. ".x" after the Vector3 variable will return the x component as a float. You may want to have a look at the Vector3 scripting reference for this instance.
Answer by Foulcloud · Jun 06, 2015 at 03:38 PM
I think you answered your own question. You will need to declare gridPositions as an array and I usually set the size before hand.
Vector3[] gridPositions;
gridPositions = new Vector3[worldHeight];
for(int x = 0; x <= worldWidth; x++) ..... rest of code
Then to access x value:
tempFloat = gridPosition[I].x;
Answer by tanoshimi · Jun 05, 2015 at 04:35 PM
You've called your list gridPositions
, so it's gridPositions[i].x
, not gridPosition[i].x
.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
instantiate problem help? 1 Answer
Vector3[]? 1 Answer
What's the proper way to see if a Vector3 List index is null? 1 Answer