Change bool in an array from another script

Hi there,

I’m not exactly too great with scripting at the moment and I was wondering if I could get some help with something. I have used a free achievement manager script and I have trimmed it down quite small to the bits which I only need. The script allows me to create an array of achievements and assign a Name(string), Description(string), Complete Icon(texture), Incomplete Icon(texture), Earned state(bool).

I have made the earned bool state public so that when I press it in the inspector, naturally the Incomplete Icon changes into the Complete Icon.

I want to be able to do the said function above but from another script and implement it on for example a mouse click. I have used AchievementManagerAll.Earned = true; from the other script (AchievementManagerAll being the achievement manager script) and this works, except it changes ALL the achievements to true instead of just the one. What I want to do is tell my other script to JUST to turn on the Earned state for 1 particular achievement in the array.

So I guess the question would be, how would I change a bool value of an array element from another script? Would I have to do something along the lines of AchievementManagerAll.Earned(“Test”, true); ? ← I know that’s wrong of course but you get the picture

I’ll provide screenshots below. All help greatly appreciated.

-Connor

[29294-array+2.png|29294]
[29293-array+1.png|29293]

Try adding something like this to AchievementManagerAll:

public void SetEarnState(int index, bool newState)
{
    Achievments[index].Earned = newState;
}

Then try setting your earn state from a different script:

AchivementManagerAll _AchivementManager = (AchivementManagerAll) GameObject.FindObjectOfType(typeof(AchivementManagerAll));
_AchivementManager.SetEarnState(0, true);

sadly it is true that this line is wrong AchievementManagerAll.Earned(“Test”, true);, because the variable Earned is not the member of class AchievementManagerAll but of AchievementAll but the class AchievementManagerAll contains a variable named Achievements of type Array means you should try something like this : AchievementManagerAll.Achievements[1].Earned = true;

the above line as pointing towards the SECOND element of the array as the indexing of an array starts from 0 like 0,1,2,3 and so on…

Hope this helps :slight_smile: