Consider a game that is somewhat similar to Risk.
I’m using Scriptable Objects, both for the continents and the areas within each content. Each area has a name and a reference to the continent and some more properties. The continent also has a name and some additional properties. Now suppose I’m able to open a window that shows the details for a single continent and its areas. How do I set-up the code, so that the continent knows what areas lay within in?
Ideally, I don’t want to give the continent a list property storing its areas, because information is then stored at two places, which leads to mistakes. But I also don’t see how I can make a function GetAreas() that will give me the list I want.
Any ideas?
@mathemaat
Personally, I would make the list, but have it populate that list at runtime (or if I needed to see the connections in the editor, automatically populate the list by running through the assets).
You can populate the list easily by having each area add itself to the continent’s list of areas in the OnEnable event. (If you needed this in the editor, having a Scriptable Object that contained a list of all the continents as well as all the areas would make it easier to process any changes you made to area location. You can use the OnBeforeSerialize event to update relationships when you change them via the inspector.)
Thanks, the following code did the trick 
In area.cs
private void OnEnable()
{
if (continent != null)
{
continent.AddArea(this);
}
}
In continent.cs
public void OnBeforeSerialize() {}
public void OnAfterDeserialize()
{
areas.Clear();
}
The latter is to prevent issues that may occur if you change the continent of an area. You don’t want the area to occur within two continents. Note that areas is a HashSet, so an area is never added twice to the same list.