Lets say I have a built-in array called myArray that is full of gameobjects.
Now lets say I’m looking at the object at myArray[27]. How do I get the index number 27 in order to say somevariable = 27?
Edit: Initially I wanted to do it with a List, but I need to do it with a built-in array (despite the title). How to do it with a List would also be helpful though. I “think” it’s with the myList.IndexOf(myList[27]), but I havn’t tested it yet
I’m thinking:
// JavaScript Syntax
var somevariable : int = myList.length;
// C# Syntax
int somevariable = myList.length;
This is just off the top of my head so you can try it out but no guarantee.
In C#:
// Declare variables
GameObject[] gameObjects;
int index = 0;
// Populate the array with something - I'm sticking all
// GameObjects in it, which you probably don't want to do
gameObjects = FindObjectsOfType(typeof(GameObject)) as GameObject[];
// Set the index value to the position you want to look at
index = 3;
// Use it to reference the array
// In this case, the below is equivalent to looking at gameObjects[3]
GameObject go = gameObjects[index];
UnityScript will be similar, you just need to change the syntax of the declarations.
Each of the above chunks are examples of single lines only - you need to put them in the correct parts of your code, as they won’t actually work as they are there.
Also, you’ll probably want to do bounds checking on the index. It can’t point past the end of the array or at any value less than 0.
You use IndexOf, he is a quick dirty example
List<string> s = new List<string> {"moo"};
int index = 0;
foreach (string r in s)
{
index = s.IndexOf(r);
Debug.Log(index);
}
Hi angrypenguin, your solution works with what I said in my post, the only problem is I stated my post wrong sorry lol. I actually will not know what the index number is – or I guess it kind of has to be relative, because it will change each update.
For example,
I will know that I’m working with myArray[27]. And I know I want somevariable = 27. But I can’t set somevariable = 27, because it will change the next update. Next update it could be myArray[305], and somevariable will need to equal 305. So, I need it to be relative basically. I need to retrieve it somehow, instead of setting it statically. I hope that made sense.
To EliteMossy: Thank you, that will give me the index number when I’m working with a list, but I changed my post to work with a built-in array. Is there a way to do the same thing with a built-in array?
So, instead of using a built-in array, I decided to change the array to a generic List specifically so I could use the “IndexOf” functionality, and it seems to be working. So the issue is resolved, however I’d still be interested to know how to do it with a built-in array, just out of curiousity.
So thanks for the help everyone
I guess I was just trying to be a lazy programmer too, if there was a simple way to do it with built-in arrays.
class Program
{
static void Main(string[] args)
{
//To mess with EliteMossy
var data = new List<string> { "moo", "moo", "moo2" };
foreach (string datum in data)
Console.WriteLine(data.IndexOf(datum));
Console.ReadLine();
//To assist Velo222
var array = data.ToArray();
foreach (string datum in array)
Console.WriteLine(Array.IndexOf(array, datum));
Console.ReadLine();
//To show off
foreach (string datum in array)
Console.WriteLine(array.IndexOf(datum));
Console.ReadLine();
}
}
public static class Helper //enables third example
{
public static int IndexOf<T>(this T[] array, T item)
{
return Array.IndexOf(array, item);
}
}
I was tired when i wrote my answer 