Strange console error

I met strange error while trying to upgrade to 2022.3 > 2023.2.

I finally found the file that cause of this problem from a huge amount of scripts.
See my stupid code for how the problem might occur.

[System.Serializable]
public class ValueCondition<T>
{
    public enum ConditionEnum { EQ, NE, GT, GE, LT, LE, NOTUSE }
    [DisableIf("Condition", ValueCondition<int>.ConditionEnum.NOTUSE)] public T Value;
    ...

I am trying to access ConditionEnum in own, but why, accessing it via an int generic class.
No problem with 2022.3, but error on 2023.2, so more obvious error would be helpful.

Btw I fixed as below.

using ConditionEnum = ValueCondition.ConditionEnum;
public static class ValueCondition
{
    public enum ConditionEnum { EQ, NE, GT, GE, LT, LE, NOTUSE }
}
[System.Serializable]
public class ValueCondition<T>
{
    [DisableIf("Condition", ConditionEnum.NOTUSE)] public T Value;
2 Likes

Why are you doing this:

Why do you define a nested type just to use a using alias statement? You do know that the enum keyword defines a new type. So why don’t you just do:

public enum ConditionEnum { EQ, NE, GT, GE, LT, LE, NOTUSE }

Currently you kinda abuse that static class as some kind of namespace for your enum type.

Note that nested types inside a generic type are distinct from one another.

So in your original code the type ValueCondition<int>.ConditionEnum. is a completely separated type from ValueCondition<string>.ConditionEnum. or ValueCondition<object>.ConditionEnum.. So it didn’t make much sense to define your enum as a nested type in the first place.

If you want to keep your namespaces clean, actually use namespaces.

ps: you used the tag “feature request”. How exactly is that a feature request?

2 Likes

In fact, the class in namespace. Here is the full code.

using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine.Assertions;
namespace Nnfs.Serializable
{
    using ConditionEnum = ValueCondition.ConditionEnum;
    public static class ValueCondition
    {
        public enum ConditionEnum { EQ, NE, GT, GE, LT, LE, NOTUSE }
    }

    [System.Serializable]
    public class ValueCondition<T>
    {
        [LabelWidth(40), HorizontalGroup("1", 0.35f), DisableIf("Condition", ConditionEnum.NOTUSE)] public T Value;
        [LabelText(""), HorizontalGroup("1", 0.4f), ValueDropdown("@Editor_DropDownList()")] public ConditionEnum Condition;

        public bool Compare(T rhv)
        {
            switch (Condition)
            {
                case ConditionEnum.EQ: return Comparer<T>.Default.Compare(Value, rhv) == 0;
                case ConditionEnum.NE: return Comparer<T>.Default.Compare(Value, rhv) != 0;
                case ConditionEnum.GT: return Comparer<T>.Default.Compare(Value, rhv) > 0;
                case ConditionEnum.GE: return Comparer<T>.Default.Compare(Value, rhv) >= 0;
                case ConditionEnum.LT: return Comparer<T>.Default.Compare(Value, rhv) < 0;
                case ConditionEnum.LE: return Comparer<T>.Default.Compare(Value, rhv) <= 0;
                case ConditionEnum.NOTUSE: return true;
                default: Assert.IsFalse(true); return false;
            }

        }
        public static ValueDropdownList<ConditionEnum> Editor_DropDownList()
        {
            return new ValueDropdownList<ConditionEnum>()
            {
                {"== *",ConditionEnum.EQ}, {"!= *",ConditionEnum.NE}, {"> *", ConditionEnum.GT}, {">= *", ConditionEnum.GE}, {"< *", ConditionEnum.LT}, {"<= *", ConditionEnum.LE}, {"NOTUSE", ConditionEnum.NOTUSE}
            };
        }
    }
}

This namespace of only this ValueCondition class is not wrapped, and I don’t want.
There are many other definitions in this Nnfs.Seriazable namespace, so without much thought, I put enum to something that related ValueCondition class(I know ValueCondition and ValueCondition are different),
but a following looks better if only enum.

namespace Nnfs.Serializable
{
    using ConditionEnum = ValueCondition.ConditionEnum;
    namespace ValueCondition
    {
        public enum ConditionEnum { EQ, NE, GT, GE, LT, LE, NOTUSE }
    }
...

I know this is not a common case, but I thought it would be better to make the error displayed in the console easier to understand.