Search code examples
c#unity-game-engineitween

Destroy gameObject oncomplete


I am using this script to destroy a gameObject oncomplete, but it doesn't work.

void Jump()
{
    if (currentCube.transform.position.y == 0f) 
    {
        iTween.MoveTo (currentCube, iTween.Hash ("y", currentCube.transform.position.y - 15f, "time", 0.8f, "oncomplete", "DestroyOnComplete", "oncompletetarget", currentCube, "easeType", "easeInCubic", "loopType", "none", "delay", 0));
    }
}

public void DestroyOnComplete()
{   
    Destroy (currentCube);
    Debug.Log ("Destroyed " + currentCube);
}

Does anyone know why this doesn't work?


Solution

  • From what I see your script is not attached to currentCube and you're trying to invoke DestroyOnCompleted on currentCube. Try something like this :

    iTween.MoveTo (
        currentCube,
        iTween.Hash (
            "y",
            currentCube.transform.position.y - 15f,
            "time",
            0.8f,
            "oncomplete",
            "DestroyOnComplete",
            "oncompletetarget",
            gameObject, // here you had `currentCube`, you can even try with `this` instead
            "easeType",
            "easeInCubic",
            "loopType",
            "none",
            "delay",
            0
        )
    );