Author Topic: (MonoBehaviour) callback.Target InvalidCastException  (Read 5327 times)

dominus85

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 31
    • View Profile
(MonoBehaviour) callback.Target InvalidCastException
« on: January 10, 2014, 12:41:11 PM »
I have an error when using Tweens with listeners that belong to custom classes, not MonoBehaviours

InvalidCastException: Cannot cast from source type to destination type. EventDelegate.Equals (System.Object obj) "EventDelegate.cs:118"

I have a class: Custom
I have a tween on a MonoBehaviour
I access the tween from the Custom class
I add a delegate for the tween that is a method in my Custom class
EventDelegate.cs:118 tries to check for the existance of that delegate (happens only with REFLECTION_SUPPORT on)

The fail is because of (MonoBehaviour)callback.Target -> This causes an InvalidCastException

The fix:

Please check for callback.Target is MonoBehaviour && mTarget == callback.Target as MonoBehaviour

OR simpler

mTarget == callback.Target as MonoBehaviour

The as cast will evaluate to null but won't throw an exception



ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: (MonoBehaviour) callback.Target InvalidCastException
« Reply #1 on: January 10, 2014, 05:21:19 PM »
Fair enough. I'll need a reminder of this after the 16th.

dominus85

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 31
    • View Profile
Re: (MonoBehaviour) callback.Target InvalidCastException
« Reply #2 on: January 18, 2014, 01:58:57 PM »
I hope you had a nice vacation :) Haven't really had the time to test the new release, so this is just in case reminder..

Thanks

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: (MonoBehaviour) callback.Target InvalidCastException
« Reply #3 on: January 18, 2014, 10:08:19 PM »
Thanks for the reminder, I will add the change to f2.