Finding specific index of an array
I just cant seem to solve this little problem. I know that there is a lot of documentation on this subject, but none of those seems to help me.
Just to simplify the problem, lets say i have a few Buttons (unitys UI stuff) which all are part of an array. When i click one of the buttons, i want to know what that certain buttons index number is from the array. From what i have found out, alot of people say that you could use System.Array.IndexOf. But i dont see how that could help me, or in other words, i dont get it to work. Does anyone have any ideas?
Answer by alankemp · Apr 24, 2017 at 04:12 PM
If we have an array on integers with some content:
int[] numbers = { 15, 42, 3, 126, 999 };
And we wanted to know what index the number 42 occured at, we could write a loop to find it like this:
int indexOf42 = -1;
for (int i = 0; i < numbers.Length; i++)
{
if (numbers[i] == 42)
{
indexOf42 = i;
break;
}
}
Console.WriteLine("indexOf42={0}", indexOf42);
Remembering that indicies into an array are 0 based, this will print "indexOf42=1".
You can use Array.IndexOf to do the same work. To find the index of 999 in the array:
int indexOf999 = Array.IndexOf(numbers, 999);
Console.WriteLine("indexOf999={0}", indexOf999);
yes, that part i have got the hang of. The problem is that i do not know how to get the certain index of the button i pressed. Just for an example. I have these 5 buttons, and when i press the second button from left it has the index number of 1. In other words, i know what index number every button have when i look at them. Just, how do i tell the script which index number it have?
i can throw in my script here which assigns characters to different buttons.
public class Button$$anonymous$$anagerCharacters : $$anonymous$$onoBehaviour {
public GameObject[] buttonList;
public GameObject[] partyList;
private void Update(){
AddCharToButton();
}
// This is where i assign both the Name and the Sprite of the Character.
public void AddCharToButton() {
string[] characterNames = System.Enum.GetNames(typeof(Character$$anonymous$$anager.CharacterNames));
// Character Names
for (int i = 0; i < buttonList.Length; i++) {
if (i < characterNames.Length - 1)
buttonList[i].GetComponentInChildren<Text>().text = characterNames[i + 1];
else
buttonList[i].GetComponentInChildren<Text>().text = characterNames[0];
}
// Character Sprites
for (int i = 0; i < buttonList.Length; i++)
{
if (i < characterNames.Length - 1)
buttonList[i].GetComponent<Image>().sprite = Resources.Load("Characters/" +
characterNames[i + 1],
typeof(Sprite)) as Sprite;
else
buttonList[i].GetComponent<Image>().sprite = Resources.Load("Characters/" +
characterNames[0],
typeof(Sprite)) as Sprite;
}
}
// This is where i want to know the index number of the certain button i pressed!
public void AddCharToParty() {
}
}
Wow, sorry for how hard this script is to read.
I usually create a small component, say called "CharacterButton" in this case, and add it to the button game object, something like this:
public class CharacterButton : $$anonymous$$onoBehaviour
{
public int Index;
public void Init(int index)
{
Index = index;
// code from AddCharToButton could be moved here
}
}
Then when you create the button use AddComponent() to add this class, and call .Init(< the index>) to initialise it.
Later you can use GetComponent() and then check .Index for its index in the array.
Yeah, this actually seems like a nice workaround! Unity should add something like Array.FindIndexNumber(); (or something similiar) just to get around all this. But what do i learn from being lazy, huh? ;)
Thank you for the help my good sir!
Your answer
Follow this Question
Related Questions
How can I find the index of an object in an array from that object's script? 0 Answers
Parameter Name: Index 0 Answers
C# array error index is zero but when i try to assign it to variable it suddenly change to 3 0 Answers
C# ArrayList Accessing and RemoveAt? 0 Answers
Another Physics.OverlapSphere question 0 Answers