For loop within Serializable class?
I currently have this, however if I add the for loop it doesn't work . Could anyone point me out any tips?
What do you mean it doesn't work, no one knows what is in player parameter(of type UI_Holder) of the constructor. No one knows why you're using a loop and breaking after the first iteration. You need to supply more information then it doesn't work, is there an exception, are you having issues because only the first element is being applied(that is because you're breaking out of the for loop at the end of the first iteration). If you want to be helped, help yourself by explaining in detail what is going on, what typical data exists in the player variable and what you're expectations are.
Okay. Answering your questions:
"why you're using a loop and breaking after the first iteration" - A suggestion made by someone. Didn't work, but since I was still looking for a solution, forgot to remove it.
"more information then it doesn't work" - I have Lists of containers that each have their own points and name. As such, I searched and found you cannot save prefabs, however you can store their variable values in Lists and then respawn them using those values.
As such, I need a for loop to go through the whole List of prefabs in order to get each value of each list element to store them.
There are no exceptions, it works fine with single variables, but when I try to use a for loop to store something, it doesn't work. So if I do
[Serializable]
public class PlayerData
{
public bool SetupComplete;
public PlayerData(UI_Holder player)
{
SetupComplete = player.isSetupComplete;
}
}
If I use my Load function, it will load this bool value just fine. However, If I try to store values inside a list using a for loop the game saves no values at all.
[Serializable]
public class PlayerData
{
public int[] ChildrenPoints;
public string[] ChildNames;
public PlayerData(UI_Holder player)
{
for( int i=0; i < ChildNumber; i++)
{
ChildrenPoints[i] = player.Children[i].GetComponent<ChildScrollContainer>().ChildPoints;
ChildNames[i] = player.Children[i].GetComponent<ChildScrollContainer>().ChildName;
}
}
}
Hope this was able to explain the issue better.
Answer by hexagonius · Nov 17, 2016 at 10:54 PM
tried doing this before looping?
ChildNames = new string[ChildNumber];
ChildrenPoints = new int[ChildNumber];
Your answer
Follow this Question
Related Questions
Exlcude fields from JsonUtility but still serialise them? 1 Answer
Arrays not serializing 1 Answer
Serialization Placeholder Value for Different Fields? 1 Answer
Serializing pickups? 0 Answers