I’ve spent the last 8 hours looking around (I’m new) trying to find the best approach at making a specific puzzle game.
It will be a grid of 3x7. at one side you have 3 units and the other end will have enemies slowly making their way down. I want to be able to swap the unit positions and have specific abilities that affect other grid tiles.
After an entire day of searching, I’m gathering maybe a 2D array is the best approach? I really have no idea. Any help or even a rough direction would be appreciated.
You need to make a 2D array, with 3 rows and 7 columns. Than with a for loop set each element of array equal to a block instantiation.
Then you can spawn your units in these blocks and you access them with the array. This code is not tested but should give you an overall direction.
GameObject[,] grid = new GameObject[7,3];
void Start () {
for(int i = 0; i < 7; i++){
for(int y = 0; y < 3; y++){
grid[i,y] = (GameObject)Instantiate(Resources.Load("Cube"),new Vector3(i,y,0),Quaternion.identity);
}
}
}
1 Like
Okay sounds good. Simple enough. How would I swap objects, say if I wanted to swap [1,2] with [2,2]? With that I should be set on my learning adventure.
Ok. heres how. You save [1,2] in another temporary variable. Then you swap like this:
GameObject[,] grid = new GameObject[7,3];
void Start () {
for(int i = 0; i < 7; i++){
for(int y = 0; y < 3; y++){
if(i == 1 && y == 2){
//This block is going to be manually placed, continue means that is going to skip this block
continue;
}
grid[i,y] = (GameObject)Instantiate(Resources.Load("Cube"),new Vector3(i,y,0),Quaternion.identity);
grid[i,y].name = "Cube" + i + "," + y;
}
}
//The block we skipped we put here.
grid[1,2] = (GameObject)Instantiate(Resources.Load("Cube1"),new Vector3(1,2,0),Quaternion.identity);
Debug.Log(grid.Length);
}
void Update(){
if(Input.GetMouseButton(0)){
Debug.Log ("Testing");
GameObject tmpry = grid[1,2];
grid[1,2] = grid[2,2];
grid[2,2] = tmpry;
reposition();
}
}
void reposition(){
for(int i = 0; i < 7; i++){
for(int y = 0; y < 3; y++){
grid[i,y].transform.position = new Vector3(i,y,0);
}
}
}
In the script i made 2 different blocks. And i tried to swap them and everything is working perfectly fine.
Okay, sweet. I’ve got it running.
Now, I have my grid down and can Instantiate gameobjects where I want. I have run into another hitch.
So I have my grid with units instantiated at either [6,0],[6,1] or [6,2]. Using custom enemy scripts on the objects I can easily move them step by step down the field each turn, but how do I change their array index also (so from [0,6] → [5,0])? The grid is located in a separate ‘GridManager’ script.
Here is what I have on my enemy script:
using UnityEngine;
using System.Collections;
public class Poring : MonoBehaviour {
void Update () {
Vector3 curPos = transform.position;
if (GameManager.currentState == GameManager.GameState.ENEMYTURN) {
Vector3 newPos = new Vector3(curPos.x - 1, curPos.y, curPos.z);
transform.position = newPos;
}
}
}
My brain is blanking, but pseudo would be something like:
get gameobject position in grid(in gridmanager).
change grid[posX,posZ] to grid[posX -1, posZ]
Just not sure how to go about it
Like this?
grid[posX -1, posZ] = grid[posX,posZ]
grid[posX,posZ] = null