- Home /
finding an objects index in a multi dimensional static array?
ok so i have a selector that tells me the name of my object in my multi-dimensional array, but i want to know how i can have it send me back the index of that object. can anybody help me out?
Depends on how your selector works! Presumably, to get the name of the object, you had to have the index at some point anyway- so return that ins$$anonymous$$d!
I think you can use IndexOf(object), but that might only work for generic lists (I can't remember...)
Answer by syclamoth · Oct 10, 2011 at 04:44 AM
Given that you know the transform (I assume it is an array of transforms?) you can use
var xPos : int = -1;
var yPos : int = -1;
for(var i : int = 0; i < array.GetLength(0); i++)
{
for(var j : int = 0; j < array.GetLength(0); j++
{
if(selectedTransform == array[i, j];
{
xPos = i;
yPos = j;
break;
}
}
if(xPos != -1)
{
break;
}
}
And then you can use xPos and yPos! If they return -1, it means the selected transform isn't in the grid (oops);
break tells it "Ok I'm done with this now, don't bother looping through the rest of the iterations. kthxbai!". I've got it there to save time, although you don't techincally need it.
if i where to turn this into a function i would have it return the xPos and the yPos right? and then i would send it my array that i have? thats it?
thanks dude if i could give you a million credit points i would right now! you have been so helpful!
$$anonymous$$ind of? You can't return two values at a time without defining a type which contains them, and you'd need to send it both the array, and the transform you're searching for. I'm really not sure what the context of what you're doing is... (well obviously it's your TD that you are making, but more specifically)
Your answer
Follow this Question
Related Questions
Swapping elements of an array 1 Answer
Need a Custom array Index to look up gameobject tags? 2 Answers
Array of GameObjects 4 Answers
Removing objects from an array 2 Answers
How to remove objects from a list ? 3 Answers