Harvey Parker-Acheson Harvey Parker-Acheson - 1 month ago 9
C# Question

Referencing components of objects from an array in C#

I'm trying to make a "thing" that checks/unchecks a specific object's interactable checkbox from an array by using player pref integers. The problem I'm having is I can't seem to reference specific objects from an array, please help.

Here's some script:

//This part is from the Start function.

for (int i = 0; i < buttons.Length; i++) {

if (PlayerPrefs.GetInt("button" + i) == null) {

PlayerPrefs.SetInt("button" + i, 1);

}

if (PlayerPrefs.GetInt("button" + i) == 1) {

button.interactable = true;

} else {

button.interactable = false;

}

}



void Update () {

for (int i = 0; i < buttons.Length; i++) {

if (PlayerPrefs.GetInt("button" + i) == 0) {

button.interactable = false;

}

}

}


The areas where you can see button.interactable = true/false are where I'm having problems.

Answer

If you don't have button defined somewhere else; I assume you are missing the concept of array index accessors; you might want to use buttons[i] instead of button.