I’m creating a grid based puzzle game by using a 2D array that creates copies of some prefab objects. That works perfectly fine and I am able to get the game objects created and put into the level. However, I need help making it so that each instance of the prefabs know where they are in the array. I need them to be able to tell if they are at [0,0], [2,1, [1,0], etc so that they can modify their neighbors based on what is there. I’ve tried giving variables to each object and then assigning them once they are created but that didn’t seem to work at all. And obviously I can’t just rely on my nested for loop variables because those will be maxed at the end and useless. I’m a bit rusty with coding at the moment as well but I’m hoping there’s an easy solution to get each copy to remember where they are in the array. Here is a copy of my array and level set up.
//This will create a 2D array that will hold some spots and data for various squares
//10 represents a blank square where a player can place an orb
//0 represent a square filled with a blank orb
var startX : int = -1; // this is the left hand side of the puzzle grid
var startY : int = 1; // this is the top of the puzzle grid
var gridArray = [
[10, 0, 10],
[10, 0, 0],
[0, 0, 10] ];
///This will end up placing the correct objects in the level based on gridArray
var r : int;
var c : int;
var gridW : int = gridArray.length; //gets the width of the gridArray for the for loops
var gridH : int = gridArray[0].length; //gets the height of the gridArray for the for loops
for(r = 0; r < gridW; r++)
{
for(c = 0; c < gridH; c++)
{
var currentX = startX + (r*.96); // this moves newsprites right for each new column being created
var currentY = startY - (c*.96); // this moves new sprites down for each new row being created
// 1 pixel seems to be .01 unity units. 100 pixels would be 1 unit
// r and c are reversed in the if statements so the squares are placed left to right
if(gridArray
[r] == 10)
{
Instantiate(obj_BlankSpot, Vector3 (currentX, currentY, 0), Quaternion.identity); //this object is a blank square where a player can place an orb
blankSpotScript.xCoord = c; //blankSpotScript allows me to access the x and y variables in the script attached to obj_BlankSpot
blankSpotScript.yCoord = r; //blankSpotScript allows me to access the x and y variables in the script attached to obj_BlankSpot
//Need this to save it's unique position in gridArray[c][r]
}
else if(gridArray[c][r] == 0)
{
Instantiate(obj_PlacedBlankOrb, Vector3 (currentX, currentY, 0), Quaternion.identity); // this object is a square that hold a blank orb that can be broken
blankOrbScript.xCoord = c; //blankOrbScript allows me to access the x and y variables in the script attached to obj_PlacedBlankOrb
blankOrbScript.yCoord = r; //blankOrbScript allows me to access the x and y variables in the script attached to obj_PlacedBlankOrb
//Need this to save it's unique position in gridArray[c][r]
}
}
}
////////
Update:
////////
So I tried making some adjustments to the code and assigning the individual squares different variables that were set to r and c at their time of creation but the values are getting all mixed up and it's not working out how I intended. Basically the blank squares and blank orbs now have an xCoord and yCoord value that I was trying to set to r and c. I'm thinking that maybe when it's assigning the values to xCoord and yCoord that it's choosing the wrong prefab instances?
I put the new if/instantiate code up above. Any ideas? I guess I could try and figure out each instance ID and then specify that instance ID in the variable assignment somehow, but I'm not sure how I would go about doing that.
////////Second Update://///////
Since I was having issues with getting the values for each prefab instance when trying to assign the values in the for loops, I tried having the prefab instances themselves get the values from the for loop variables r and c themselves. However, even though r and c are obviously incrementing in order to go through the loops, the variables in the prefab instances always stay at 0. Here is the code for one of the prefabs:
var selectedOrbScript : scr_gridArray;
var xCoord : int = selectedOrbScript.c; //sets xCoord = to the c value in the loops for the 2D array
var yCoord : int = selectedOrbScript.r; //sets yCoord = to the r value in the loops for the 2D array
function OnMouseDown ()
{
print (xCoord);
print (xCoord);
}
When I mouse click on them, the values are 0 and 0 for all 9 grid spaces and i can't figure out why. I've searched for the answer to this on here and I can't find anything specifically saying how to just keep each prefab position separate and identifiable in the array. This is the only thing keeping me from moving forward and finishing the project at this point so if anyone has an idea, please let me know.