- Home /
Possible to add array values within a to a script at runtime?
Hello, new coder here, been self learning for about a year now, and have never had to ask a question before, as I was always quite savvy enough to search for examples and find my own answers, however with this one, I am really stuck, cause I cant get my head around the logic (if its even possible)
BACKGROUND: working on an achievement system that has meta achieves that are dependent upon other normal achieves being completed first. I have a few variables. 1) string Title 2) string Description 3) string Category 4) int Points 5) int IconIndex
plus a dependency value that is set to null if there is none. however this is where I am running into a problem.
I know I can get the count of my dependency array , so I know how many dependent string values I have, but how can I actually loop through them and add them to code..
see examples:
My List function: ///////////////////////////////////////////////////////////////////////////////////////
public List<AchievementInfo> AchievementLists = new List<AchievementInfo>();
[System.Serializable]
public class AchievementInfo{
public string Title = "Title";
public string Description = "Enter a Description";
public string Category = "General or Other";
public int Points;
public int SpriteIndexed;
public string[] dependencies;
}
////////////////////////////////////////////////////////////////////////////////////
the code I need to trigger:
CreateAchievment (STRING CATEGORY,STRING TITLE,STRING DESCRIPTION,INT ICONINDEX,STRING DEPENDENCIES (if NULL THEN VALUE = 0 *however if not NULL then see below for example);
CreateAchievment (STRING CATEGORY,STRING TITLE,STRING DESCRIPTION,INT ICONINDEX,new string[]{STRING TITLE(1) NEEDED, STRING TITLE(2) NEEDED,etc);
so you can see that this achieve may need one or two or twenty other achieves (TITLES) in order to trigger the earning of that meta achieve.
now in my code on start up I have: /////////////////////////////////////////////////////////
void Start ()
{
for(int cnt = 0; cnt < AchievementLists.Count; cnt++)
{
int icount = AchievementLists[cnt].dependencies.Length;
if (icount > 0)
{
foreach (string x in AchievementLists[cnt].dependencies)
{
Debug.Log ("dependents: "+ x);
}
Debug.Log("DEENTitle: " + AchievementLists[cnt].Title + " Decription:" + AchievementLists[cnt].Description + " Category: " + AchievementLists[cnt].Category + "Points: " +AchievementLists[cnt].Points.ToString() + "SpriteIndex: "+ AchievementLists[cnt].SpriteIndexed.ToString());
}
else{
CreateAchievment (AchievementLists[cnt].Category,AchievementLists[cnt].Title,AchievementLists[cnt].Description,AchievementLists[cnt].Points,AchievementLists[cnt].SpriteIndexed);
//Debug.Log("Title: " + AchievementLists[cnt].Title + " Decription:" + AchievementLists[cnt].Description + " Category: " + AchievementLists[cnt].Category + "Points: " +AchievementLists[cnt].Points.ToString() + "SpriteIndex: "+ AchievementLists[cnt].SpriteIndexed.ToString());
}
}
now this works fine if my array for dependencies is set to none (0), however how can I possibly plug in the number of dependencies into that same script line above?
I thought I could limit the amount of achieves needed for a meta achieve so I could do
if (List.count == 1) { CreateAchievment (STRING CATEGORY,STRING TITLE,STRING DESCRIPTION,INT ICONINDEX,STRING DEPENDENCIES#1) }
if (List.count == 2) { CreateAchievment (STRING CATEGORY,STRING TITLE,STRING DESCRIPTION,INT ICONINDEX,STRING DEPENDENCIES#1,STRING DEPENDENCIES#2) }
if (List.count == 3) { CreateAchievment (STRING CATEGORY,STRING TITLE,STRING DESCRIPTION,INT ICONINDEX,STRING DEPENDENCIES#1,STRING DEPENDENCIES#2,STRING DEPENDENCIES#3) }
etc etc... which right now seems to me the only thing that could work...
however, I was hoping of a more elegant solution without having to limit the amount of meta achieves we may want to have in our game design.
I hope I made myself clear enough... I am not used to asking questions of this depth.
also note the above code isn't real/compiled, its just there as a visual representation of what I am hoping to achieve.
$$anonymous$$aybe try using a foreach()
foreach(String Title in dependencies) {
//doSomething
//Title is the current element in dependencies,
//and it will loop through every String in dependencies
}
Your answer
![](https://koobas.hobune.stream/wayback/20220611232139im_/https://answers.unity.com/themes/thub/images/avi.jpg)