Cannot convert object '' to type 'UnityEngine.GameObject'

desukarhu

Member
Hey,

I keep running into this error when using the Inventory Manager to edit GameObject attributes. It happens when I have a GameObject Attribute with some value in it and then I try to clear it.

For example I have "Particles" GameObject attribute field and I add "FireEffect" GameObject in it. Then I change my mind later and I want to clear the field, but then I get the "Cannot convert object '' to type 'UnityEngine.GameObject'" error and the attribute doesn't get cleared. I have to open the ItemDefinition in the Unity inspector with debug mode on to clear it from the Unity Objects list.

Also when I look at the ItemDefinition with debug mode I see there's two "FireEffect" gameobjects in the "Unity Objects" list, is this normal?

How do I fix this permanently?
 
You are right, I was able to replicate the issue.

Here is the fix:
Change the following function in the Attribute.cs file.
C#:
public override void SetOverrideValueAsObject(object overrideValue,
    bool setVariantTypeToOverride = true, bool reevaluate = false)
{
    if (!(overrideValue is T value)) {
        //Flag Enum values cannot be converted the same way as the rest.
        Type type = typeof(T);
        if (type.IsEnum) {
            value = (T) Enum.ToObject(type, overrideValue);
        } else {
            var changeType = Convert.ChangeType(overrideValue, typeof(T));
            if (changeType is T convertedValue) {
                value = convertedValue;
            } else if (overrideValue == null) {
                value = default(T);
            } else{
                Debug.LogWarning($"Cannot convert object '{overrideValue}' to type '{type}'");
                return;
            }
            
        }
    }
    SetOverrideValue(value, setVariantTypeToOverride, reevaluate);
}
 
Top