Hi people.
I try to make a two dimensional array but somehow it manages to create a 3 dimensional array. First of all, where does this extra dimension come from, secondly, how can every index contain all the indexes including the ones made after it and lastly, how do I get this fixed?
Please inspect the following code:
This is in File1 which is attached to the main playing board:
var homeBaseBoard : HomeBaseCode; //image players stand on at start
private var homeBase = new Array();
function Start ()
{
if (players < 2) players = 2; //always at least 2 opponents to pit against each other
if (segments == 0) segments = 16; //just to have some segments
while (segments % players > 0) segments++; //equal segment count per player
homeBase.length = players;
initMarkers();
}
function initMarkers()
{
for (i=0; i < players; i++)
{
initHomeBase(i, rotateHomeBaseBy * i);
}
}
function initHomeBase(base, rotation)
{
homeBase[base] = Instantiate(homeBaseBoard);
homeBase[base].initAvatars(base, rotation);
}
Now, the rest is inside the file HomeBaseCode which is attached to the prefab that is instantiated in the initHomeBase function:
private var avatars = new Array();
var maxPieces : int = 3;
class Avatar
{
var shape : moveToSegment;
var current : int;
var next : int;
var canMoveIn : int;
var goal : int;
}
function initAvatars(PC, rotation)
{
avatars.length = maxPieces;
for (i = 0; i < maxPieces; i++)
{
avatars[i] = new Avatar();
var newPiece : Avatar = avatars[i];
newPiece.shape = Instantiate(playerPiece, transform.position,transform.rotation);
newPiece.shape.transform.Rotate(0,180,0);
newPiece.shape.name = "Player" + PC + "Avatar"+i;
}
}
Alright, now, what I intended was to get this structure:
homeBase[amtOfPlayers].avatars[amtOfAvatars].someDetail
so could someone please explain to me why I get this structure:
homeBase[amtOfPlayers].avatars[amtOfAvatars][amtOfAvatars].someDetail
For every avatar each player has, only 1 model gets created in the inspector, so the code obviously creates the correct amount of play pieces, BUT (and here is the kicker) if you inspect the code carefully, you will notice that every play piece has a unique name. Well, in the inspector they show up as:
Player0Avatar0
Player0Avatar1
Player0Avatar2
Player1Avatar0
Player1Avatar1
Player1Avatar2
but in this code
for (a=0; a < homeBase.length; a++)
for (b=0; b < homeBase[a].avatars.length; b++)
for (c=0; c < homeBase[a].avatars[b].length; c++)
textarea.text += homeBase[a].avatars[b][c].shape.name + "\n";
they appear as
Player0Avatar0
Player0Avatar1
Player0Avatar2
Player0Avatar0
Player0Avatar1
Player0Avatar2
Player0Avatar0
Player0Avatar1
Player0Avatar2
Player1Avatar0
Player1Avatar1
Player1Avatar2
Player1Avatar0
Player1Avatar1
Player1Avatar2
Player1Avatar0
Player1Avatar1
Player1Avatar2
So it would appear that for every avatar created, a new array was created containing that avatar and each and every other avatar crated after that. How in the world is that possible???
The only thing I can think of is the fact that Unity doesn’t like using class vars that are not staticly typed so Joachim suggested I do this:
avatars[i] = new Avatar();
var newPiece : Avatar = avatars[i];
newPiece.shape = Instantiate(playerPiece, transform.position,transform.rotation);
Is it possible that this line of code does NOT make newPiece act as a pointer but in fact creates new arrays? I can’t see this as being possible because then the array of 4 items would have an array of 1 item followed by an array of 2 items followed by an array of 3 etc, but in my sample code above it clearly shows that all array indexes have the total length of the final array so how in the world is this phantom array index created???
Any input?