- Home /
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
Giving you an exact answer would require seeing more code and having a full understanding of how you did your graphics and on what game objects all the scripts live. To get you started, let me list three ways that object communicate:
The primary way is GetComponent():
http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
http://unitygems.com/script-interaction1/
Send$$anonymous$$essage() is also used.
http://docs.unity3d.com/ScriptReference/GameObject.Send$$anonymous$$essage.html
And for C# you can use Events and Delegates:
http://answers.unity3d.com/questions/600416/how-do-delegates-and-events-work.html?sort=oldest
Answer by Jessespike · Jul 16, 2014 at 04:56 PM
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);
Answer by aditya · Jul 16, 2014 at 05:46 PM
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 :)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Click To Revert to Original Texture else Destroy script help 1 Answer
How to have animation play correctly 1 Answer
Need a bit of help with my script (about 100 lines) 1 Answer
Score System help 1 Answer