- Home /
Step through an array and get the GameObjects next to it
ok so i have a gridFloor that was created with nested for loops and an instantiation, it was then put into a multidimension array. i was wonder how i would go about getting the variables around the current gameObject, but stop it when it hits a variable that is out of range, everything i have tried hasn't been working. here is what i have so far.
for(var i : int = 0; i < findTotalRows.gridX; i++)
{
for (var j : int = 0; j < findTotalRows.gridY; j++)
{
if(floorMap.map[i,j].name == this.name)
{
myX = i;
myY = j;
yNext = myY + 1;
xNext = myX + 1;
yBack = myY - 1;
xBack = myX - 1;
tileUp = floorMap.map[myX,yNext];
tileDown = floorMap.map[myX,yBack];
tileLeft = floorMap.map[xBack,myY];
tileRight = floorMap.map[xNext,myY];
}
}
}
I am pretty sure this is not very Programmer minded (if that makes sense), but i am an artist coding :) i would like to do it this way as it makes the most sense in my mind and i have visions of doing other things with it doing it this way
Hey, I'm an artist who ended up coding, and look at me now...
Syclamoth's code is using the Clamp function to make sure the value of xNext is always between zero and the number of elements in the map.
http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$athf.Clamp.html
Another way you could do it would be to use the % operator (aka the modulus operator). This operator returns the remainder when you divide two numbers. You could use it like this:
var x$$anonymous$$ax = 100;
var theIndex = ( oldX + newX ) % x$$anonymous$$ax;
That code adds two numbers together and returns a number (less than x$$anonymous$$ax) that represents what the remainder would have been if you divided them.
In other words... If you're at x-value 100 and you add 3 to it, the result will be 3 (because 100 divided by 101 equals 1 with a RE$$anonymous$$AINDER OF 3).
Using the modulus operator makes it so array index values "wrap" when they get to the limit of the array.
You would use similar logic to "wrap" a rotation so it goes back to 1 (rather than 361 degrees) after it hits 360.
Hope this helps...
That sounds like a syntax error.
$$anonymous$$y guess would be you're either using parentheses where you shouldn't or you're not using parenthesese where you should.
I think you see that in situations like this:
function name: booleanFunction
code:
if ( booleanFunction ) {
/* do stuff */
}
In the above, there SHOULD be parentheses after booleanFunction, because it's a function.
I think I might be using 'Length' wrong. It's possible that I'm not referring to the one that is appropriate for multidimensional arrays. You'll have to look that one up, I'm afraid.
Your answer
Follow this Question
Related Questions
For all elements above a certain index 1 Answer
For Loop Array Problem 1 Answer
How to deal with for loop and array? 0 Answers
For Loop Not Iterating Properly? 2 Answers
Adding an object to an array only if it is not in the array 2 Answers