Hitting Serialization depth, need a better way to access parent class
Hello, I've looked for similar questions but they all involve a game object or a monobehaviour but mine is a bit different (probably doing it the wrong way). I'm quite new to c# and Unity so apologies if this is obvious.
I have a GameManager
Class that contains an array of Character
classes, which in turn have CharacterStats
class that then have a CharacterStat
class. Based on that it's only 4 classes deep so I think I'm hitting the Unity Serialisation Depth warning because of recursion(?).
The looping/recursion (as I've read) is because I'm passing in the CharacterStats class to the Stat class because I need a reference to it. I see people getting around this by accessing the instance or the game object then finding it with GetComponent but this won't work in my case.
Is there a better way of accessing that "parent" class without passing it in as a parameter to the CharacterStat constructor?
Here is the code in question, you can see me passing in this
to the CharacterStat:
[System.Serializable]
public class CharacterStats {
public CharacterStat STA, STR, AGI;
public CharacterSkill Pistol, Bow;
public CharacterAttribute HP, NP, Speed;
public CharacterStats(Profession profession) {
STA = new CharacterStat(this, "Stamina", StatType.STA, profession.STA);
STR = new CharacterStat(this, "Strength", StatType.STR, profession.STR);
AGI = new CharacterStat(this, "Agility", StatType.AGI, profession.AGI);
Pistol = new CharacterSkill(this,"Pistol", StatType.Pistol, profession.Pistol, profession.PistolAffinitities);
Bow = new CharacterSkill(this,"Bow", StatType.Bow, profession.Bow, profession.BowAffinitities);
}
}
Answer by streeetwalker · Oct 01, 2020 at 08:51 AM
I don't believe that is your problem, but your description is not clear. Where are there any inheritance relationships? Or what do you mean by "parent - child" ? Are you talking about objects containing other objects in the hierachy?
You state that your gamemanager class contains an array of Character classes, but that language does not make sense. Perhaps you mean you have defined an array of e.g. Character[] within game manager. This will be an array of objects of type Character.
Another example: CharacterStats does in inherit from CharacterStat, according to your code.
So it is hard to tell what your problem is. What is the exact error message you are getting?
Hi @streeetwalker sorry I'm still picking up the correct ter$$anonymous$$ology. You are right, my Game $$anonymous$$anager has an array of Character
s.
None of these are inherited, I don't think "parent" was the correct term. At the top level I have a G$$anonymous$$ : $$anonymous$$onobehaviour
and that instantiates my 3 Character
classes. Those Character
classes in turn instantiate a CharacterStats
class that holds all the character stats. The stats themselves are a CharacterStat
class. The problem I'm having with depth is because I need to access the CharacterStats
class from within a CharacterStat
so the only way I managed to do that was by passing the CharacterStats
to the CharacterStat
that it was called by, as you can see from the this
in the CharacterStat
constructor.
Doing that caused the Serialisation Depth Limit warning because, as I could see from inspector, it went something like CharacterStats > CharacterStat > CharacterStats > CharacterStat
etc.
In the above, the code I posted, the CharacterSkill
is just another CharacterStat
basically.
The actual error is: Serialization depth limit 7 exceeded at CharacterStat...
I hope that makes more sense.
p.s Sorry I can't seem to get line breaks to show up on my posts.
Your answer
Follow this Question
Related Questions
Instanciating multiple objects with the same base characteristics 1 Answer
How the heck do I save this info for my characters? [Serialization Usage Question] 1 Answer
Passing in objects to class constructor 0 Answers
Can't figure out how to serialize my save data... 1 Answer
JsonUtility and Arrays - JSON must represent an object type. 0 Answers