Search Unity

Cast transform to object fail in specific generic method

Discussion in 'Scripting' started by hurjmdev, Jan 15, 2021.

  1. hurjmdev

    hurjmdev

    Joined:
    Dec 12, 2020
    Posts:
    8
    Hello,

    I try to cast Transform to object but it fail only in specific method.

    Code (CSharp):
    1.  
    2.     public abstract class StateConditionReference<E, V> : StateCondition
    3.         where E : EventReferenceType<V>
    4.         where V : class
    5.     {
    6.         protected sealed override void OnInit(SystemBinder systemBinder)
    7.         {
    8.             var systemType = Type.GetType("HDV." + so.BindSystemName);
    9.             Debug.AssertIsNotNull(systemType);
    10.             var system = systemBinder.GetSystem(systemType);
    11.             PropertyInfo pi = systemType.GetProperty(so.BindPropertyName);
    12.             Debug.AssertIsNotNull(pi);
    13.             E property = pi.GetValue(system) as E;
    14.             Debug.AssertIsNotNull(property);
    15.             property.ChangeEvent += OnChangeValue;
    16.             OnChangeValue(property.Value);
    17.         }
    18.  
    19.         protected abstract void OnChangeValue(V changedValue);
    20.     }
    21.  
    It fail at 16.

    "property.Value" has Transform value, but after casting it becomes null.

    Below is derived class.
    Code (CSharp):
    1. public class ObjectCondition : StateConditionReference<EventObject, object>
    2.     {
    3.         private ObjectConditionSO SO => so as ObjectConditionSO;
    4.  
    5.         protected override void OnChangeValue(object changedValue)
    6.         {
    7.             if ((SO.IsExecuteIfNull && changedValue == null) ||
    8.                 (!SO.IsExecuteIfNull && changedValue != null))
    9.                 IsTrue = true;
    10.             else
    11.                 IsTrue = false;
    12.         }
    13.     }
    "changedValue" should not be null but it is.

    It only happen at this moment. After initialization it works properly.

    Below is base class of generic class.
    Code (CSharp):
    1. public abstract class StateCondition
    2.     {
    3.         protected StateConditionSO so;
    4.  
    5.         private bool isTrue;
    6.         public bool IsTrue
    7.         {
    8.             get => isTrue;
    9.             protected set
    10.             {
    11.                 isTrue = value;
    12.                 TriggerEvent?.Invoke(this);
    13.             }
    14.         }
    15.  
    16.         public event Action<StateCondition> TriggerEvent;
    17.  
    18.         public virtual void Init(StateConditionSO so, SystemBinder systemBinder)
    19.         {
    20.             this.so = so;
    21.  
    22.             OnInit(systemBinder);
    23.         }
    24.  
    25.         protected abstract void OnInit(SystemBinder systemBinder);
    26.     }
     
  2. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,003
    Are you sure that it'S the transform reference that is null? I guess you talk about the "changedValue" inside your "OnChangeValue" event? You haven't verified that property.Value is not null before you pass it into the event. How do you know it's null? Based on the "IsTrue" state? Or do you get an error? If you get a null reference exception it'S more likely your "as" casting of your "so" reference. Avoid as casting whenever possible unless you pair it with a null check and you specifically want to get null back if the cast fails / is not possible. Use a normal cast whenever possible. That way you get an InvalidCastException instead of a null value.
     
  3. hurjmdev

    hurjmdev

    Joined:
    Dec 12, 2020
    Posts:
    8
    Oh you are right. I just check it is null, but in IDE it's not. It seems to trouble come elsewhere.