Simple problem. Get the index value for a list or array.

I’m simply trying to get the index number by itself from an array. (for purposes of making a comparison) I can’t seem to find a command that will just give me the value…

For example…
//made up command…
print(myList[7].valueOfCurrentIndex);
//would return just the number 7.

This way I could just…

myInt = myList[7].valueOfCurrentIndex;
//myInt becomes 7…

Is there anything that achieve this simply request in unity?

If you used a generic list, you could do it:

using System.Collections.Generic;
List<MyType> myList;
var index = myList.IndexOf(myList[7]);

You’d want to do bounds checking, etc. But this whole thing seems a bit backward. You should probably keep track of the index separately somewhere.

Is there a way to do this with Javascript?

There’s always a way! You’d have to go through the array and make a note of which one matches. But, again, something seems a little off. If you already know the index is 7 (for example), you could just set myInt=7. What are you trying to do?

I’m doing a very poor man’s 2d path solver. (which I’m not certain will work) Similar to how pieces move in games like Advance Wars or Fire Emblem, pic below.

The way I plan to do this. Create a grid of potential spots the object can move.
With a board piece selected and destination selected.
The game will path solve to that location by avoiding obstacles to get to the location.

This would be achieved by beginning from the piece position. And methodically going square by square and possibly marking and placing every square into an array that counts up. By checking every square on the game board, the destination position will eventually be reached. (snake pattern that might overlap in corners, like mowing a lawn.)

With this new array created.

Then the start and endpoints in the array would be checked to see which is higher or lower.

If the starting point is lower in the array endpoint. The game would begin going from square to square checking for the highest value to eventually reach the destination. A new array would be created and a more optimal path would be achieved. Then the piece would be animated to this array.

I think you’ll have an easier time in the long run by using the very common A* pathfinding algorithm. Here’s a good tutorial on it: Introduction to A* Pathfinding | Kodeco – the code isn’t in UnityScript, but it should be fairly easy to translate. The algorithm is what’s important.

If you just want to use Unity’s built-in navigation system, here’s a video tutorial on it.

1 Like

Javascript Equivalent :

ArrayUtility.IndexOf(array, value); // Returns position of value in array.

Example:

var abc : String[];

var indexpos : int = 0;

abc[2] = "c";
indexpos = ArrayUtility.IndexOf(abc, "c");
Debug.Log(indexpos);
1 Like

+1 to @Elson_Ng for actually providing the answer to @TinyTracker 's question. But I still recommend using a standard pathfinding algorithm. Someone else has already pulled their hair out to figure out how to pathfind, so we might as well take advantage of their sacrifice. :slight_smile: