Reputation: 1
I decided to check for duplicates in the sheet via linq, and ran into a problem. When I add objects of different types to the sheet, it still throws an error about a duplicate.
I have a store that sells cheeses, so there is an enumeration containing the types of these cheeses public class
public enum SirokSkins
{
Cheap = 0,
Sweaty,
PawPaw,
Standart,
Caramel,
Vanilla
}
And also the SirokSkinsItem class itself
public class SirokScinsItem : ShopItem
{
[field: SerializeField] public SirokSkins SkinType { get; private set; }
}
ShopContent contains all skins, it creates a list in which SirokScins are stored, and the OnValidate() method, which checks for duplicates
public class ShopContent : ScriptableObject
{
[SerializeField] public List<SirokScinsItem> _sirokSkinItems;
public IEnumerable<SirokScinsItem> SirokScinsItems => _sirokSkinItems;
private void OnValidate()
{
var sirokScinsDuplicates = _sirokSkinItems.GroupBy(item => item.SkinType)
.Where(array => array.Count() > 1);
if (sirokScinsDuplicates.Count() > 0)
{
throw new InvalidOperationException(nameof(_sirokSkinItems));
}
}
When I add objects via inspector, it throws an exception. If I was sure that this was a broken check, I would just look for a new way to find duplicates, but when I changed the ScriptableObject inheritance to MonoBehaviour, attached a script to the object and called the OnValidate() method in Update(), it stopped throwing an error. That is, it only throws an error when I add any cheese-type object there, but there are no problems when calling the method further. Help me figure it out.
Upvotes: -1
Views: 28