- Home /
determine neighboring cells on a hex grid.
I'm generating a hex grid on initialization, where a cell is given a different texture based on it's position. A cell is a plane with a hex texture that has transparent cutoffs. Cells with different textures are different prefabs, created with the Instantiate function.
This worked fine for creating a grid, but now I need each cell to know which texture(or which prefab) the closest cells are. For example, the "J" key on a standard qwerty keyboard would have to know that it is surrounded by H, U, I, K, M, and N, plus the directions those keyes are in. My current ideas are to pass a variable to script of each cell through instantiate(is this possible?) or to determine what texture is displayed at a set of coordinates(I believe this can be done through a helper camera).
I could also re-do the system for creating the grid to either rename the prefab instances upon creation(is this possible?) or to create the grid with only one prefab that changes texture after instantiate based on a variable(or some other identifier).
I am also open to any other ideas I haven't thought due to unfamiliarity with unity.
Answer by Jesse Anders · Jan 09, 2011 at 03:20 PM
My current ideas are to pass a variable to script of each cell through instantiate(is this possible?)
You can't pass argument as such, but Instantiate() returns a reference to the newly instantiated object, through which you can make any needed modifications.
I could also re-do the system for creating the grid to either rename the prefab instances upon creation(is this possible?)
Yes (see above).
(I'm not sure if that entirely solves your problem, but if not, perhaps you could edit your post and clarify the question a bit.)
I've since learned that Instantiate can return to a GameObject type variable, and I believe this is what you are suggesting. Is there anyway to assign the prefab to an array ins$$anonymous$$d of a single variable? Upon this, could Instantiate return to a multidimensional array?
You can assign the return value of Instantiate() to whatever you want (a single variable, an element in a 1-d array or multidimensional array, an element of some other type of container. etc.).
Answer by M.Larin · Oct 09, 2013 at 10:17 AM
A sphere whith radius of your hexagon side, that casted from center of hexagon, will collide only whith closest planes.