- Home /
passing an arraylist to a separate script
Say I wanted to pass an array of attributes to my "save attributes" method. I put the array together on the character generator script, then call the function: GameSettings.SaveAttributes(myAttributes[]); //I'm missing something here...
I had defined the arraylist at the top along with the other variables, and added several items such as: myAttributes.Add("Constitution: "+ _constitution); myAttributes.Add("Strength: "+ _strength); myAttributes.Add("Stamina: "+ _stamina);
Can I do it this way? Looking to save the label and the stat value generated.
In my saving script, the struct is: public static void SaveAttributes(myAttributes[] attribute){ for(int ctr=0; ctr < myAttributes.Length; ctr++){ PlayerPrefs.SetInt(myAttributes[ctr]); } } Only it's not getting the reference from the generation script.
Thanks!
Answer by Julien-Lynge · Apr 26, 2012 at 03:57 PM
As I see it, you're passing a variable called attribute of type myAttributes[]. Then, in your code, you're requesting an item from a list called myAttributes - but nowhere do I see such a list being created. I believe your code should instead read:
public static void SaveAttributes(myAttributes[] attribute)
{
for(int ctr=0; ctr < attribute.Length; ctr++)
{
PlayerPrefs.SetInt(attribute[ctr]);
}
}
Also, from the context it appears that myAttributes is being populated with strings. Then you pass this to GameSettings and there you use SetInt - shouldn't this be SetString?
It actually has a string, the name of the attribute, and an int,the value of the variable holding the attribute.
So the call to the function is okay? I'm asking for help on both fronts- the call and the function.
The type or namespace name `myAttributes' could not be found. Are you missing a using directive or an assembly reference?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
create a dynamic menu 1 Answer
Use UI Button from other Script 2 Answers
Passing a class instance to a method 1 Answer