iTween causing a Stack Overflow.

Ok, I know that I’m the one telling iTween to do something that causes a stack overflow, but I’m not sure what.

I have a script that is simply tweening a target GameObjects position when an NGUI button is clicked. It works perfect in this use case. But, I need the animation to play automatically in one case when the script starts. This is done by setting the playAutomatically bool to true in the editor. When the tween begins like this, it causes a stack overflow in iTween.

There error would be posted here if Unity didn’t have a stroke when there is a stack overflow…

Here is the script:

using UnityEngine;
using System.Collections;

public class MenuTweens : MonoBehaviour
{
    public enum AnimType
    {
        None,
        MenuIn,
        MenuOut
    }

    public enum EnableCondition
    {
        DoNothing,
        EnableThenPlay,
    }

    public GameObject target;
    public AnimType animType;
    public bool playAutomatically;
    public EnableCondition ifDisabled;
    public bool disableWhenFinished;

    public float duration = 1;
    public iTween.EaseType easeType;
    public float delay;

    void Start()
    {
        if(playAutomatically) OnClick();
    }

    void OnClick()
    {
        if (target == null || animType == AnimType.None) return;

        if(!target.active && ifDisabled == EnableCondition.EnableThenPlay)
            target.SetActiveRecursively(true);

        UIRoot root = NGUITools.FindInParents<UIRoot>(gameObject);

        switch (animType)
        {
            case AnimType.MenuIn:
                transform.position = new Vector3(0, root.manualHeight, transform.position.z);
                iTween.MoveTo(target,
                                  iTween.Hash("y", 0, "time", duration, "easetype", easeType, "oncompletetarget",
                                              gameObject, "oncomplete", "TweenComplete", "isLocal", true, "delay", delay));
                break;

            case AnimType.MenuOut:
                transform.position = new Vector3(0, 0, transform.position.z);
                iTween.MoveTo(target,
                                  iTween.Hash("y", root.manualHeight, "time", duration, "easetype", easeType, "oncompletetarget",
                                              gameObject, "oncomplete", "TweenComplete", "isLocal", true, "delay", delay));
                break;
        }
    }

    public void TweenComplete()
    {
        if(disableWhenFinished) target.SetActiveRecursively(false);
    }
}

Edit: Got the error!!!

StackOverflowException
iTween.ApplyMoveToTargets () (at Assets/iTween/Plugins/iTween.cs:4197)
iTween.TweenComplete () (at Assets/iTween/Plugins/iTween.cs:4640)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
iTween:CallBack(String) (at Assets/iTween/Plugins/iTween.cs:7051)
iTween:TweenComplete() (at Assets/iTween/Plugins/iTween.cs:4652)
UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
iTween:CallBack(String) (at Assets/iTween/Plugins/iTween.cs:7051)
iTween:TweenComplete() (at Assets/iTween/Plugins/iTween.cs:4652)

Looking at the call stack it seems iTween itself has a method called “TweenComplete”, so rename yours to something else, and of course also the call to it in your MoveTo() instructions.

You propably created an indirect infinite recursion. From the callstack i would say you start a new tween in a TweenComplete callback. I don’t think the problem is in this script. Maybe it’s involved as well, but this code alone don’t would cause a stackoverflow.

Btw: Your target variable, does it point to another object, or to itself?