cartzhang / hotween

Automatically exported from code.google.com/p/hotween
0 stars 0 forks source link

Null Reference Exception #71

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Create hotween sequences on some gameobject/transform
2. Play the sequence
3. Kill/Orphan the sequences before the tween has completed.
4. Attempt to HOTween.GetTweenersByTarget(object p_target, bool 
p_includeNestedTweens) with one of the killed, destroyed, removed objects and 
"true" for include nested.

What is the expected output? What do you see instead?
One would expect that upon not finding a matching tweener, it would return an 
empty list instead of throwing a null exception.  

Part of the issue seems to be this for loop in/near the static method 
GetTweenersByTarget(object p_target, bool p_includeNestedTweens):

int count = HOTween.tweens.Count;
    for (int i = 0; i < count; i++)
    {
        ABSTweenComponent aBSTweenComponent = HOTween.tweens [i];
        Tweener tweener = aBSTweenComponent as Tweener;
        if (tweener != null)
        {
            if (tweener.target == p_target)
            {
                list.Add (tweener);
            }
        }
        else
        {
            if (p_includeNestedTweens)
            {
                list.AddRange (((Sequence)aBSTweenComponent).GetTweenersByTarget (p_target));
            }
        }
    }

If you look closely, at some point for us, there is a tween in 
HOTween.tweens[i] that does not seem to be castable to Sequence.

The issue, I believe, is that the if statement checks to see if tweener is not 
null, however, in the else block, there is no protection against 
aBSTweenComponent NOT being castable to a Sequence.  This is in fact the line 
that is returning a null reference exception for me.

Also, I may be incorrect, but If you wanted to include a list of nested tweens 
that match the passed in target, the if/else block is incorrect.  This will 
return any tweens that match the target if the tweener is not null, OR if we 
should include nested tweens, it will then try to find tweeners by target on 
the 'aBSTweenComponent'.  

Wouldn't you want to include both the tweener if it matches/is not null AND the 
results from GetTweenersByTarget if p_includeNestedTweens was true?

Original issue reported on code.google.com by jeramie....@gmail.com on 26 Mar 2014 at 6:55

GoogleCodeExporter commented 8 years ago
Hi Jeramie,

sorry if I'm late to answer, but I'm in kind of a rush these days.

What you mean with "orphan" the Sequences? You're creating a single Sequence 
with other nested Sequences/Tweeners, and are doing something on the nested 
ones? Or you just mean you're killing the main one?

Original comment by daniele....@gmail.com on 1 Apr 2014 at 10:37

GoogleCodeExporter commented 8 years ago
Basically what seems to be happening is the HOTween.tweens array has a 
reference to something that is not castable as a Sequence.  This could happen, 
as it seems to for us, if you're manually removing child transforms from a game 
object.  Either way, for whatever reason, HOTween's tweens array has something 
in it that will throw a null reference exception if you attempt to cast it as a 
(Sequence) and use .GetTweenersByTarget.

By "orphan" here, I'm referring to iterating over a gameobject's child 
transforms and setting their parent transform to null.  

Regardless, In your code you first check to see if tweener != null before 
trying to add it to the list.  You might want want to do the same type of null 
check when you try to cast it as a sequence to use .GetTweenersByTarget.

Original comment by jeramie....@gmail.com on 1 Apr 2014 at 4:58