SortingGroup[] sortingGroups;
SortingGroup[] SortingGroups => sortingGroups ?? (sortingGroups = GetComponentsInChildren());
void OnEnable()
{
foreach (SortingGroup sortingGroup in SortingGroups)
sortingGroup.enabled = true;
}
void OnDisable()
{
foreach (SortingGroup sortingGroup in SortingGroups)
sortingGroup.enabled = false;
}
The script with the similar code should be attached to the root of pooled object.
Hope, it will help you.
akianto, Jul 18, 2020
Still happening in 2020.1.10 ... looks like this is caused, for me, by creating an object from a pool and setting one of its children to active in the same frame. Happens very infrequently, added a sorting group to the parent object in an attempt to hack around this, will update if this doesn't work.
https://forum.unity.com/threads/invalid-sortinggroup-index-set-in-renderer.586450/#post-6462529