Demigiant / dotween

A Unity C# animation engine. HOTween v2
http://dotween.demigiant.com
Other
2.35k stars 350 forks source link

Destroy GameObject that is going along a path without triggering error #487

Closed Setmaster closed 3 years ago

Setmaster commented 3 years ago

I'm getting this error: Target or field is missing/null () ► The object of type 'Transform' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. when I destroy a gameobject that created a path and executes it with this script:

    List<Waypoint> waypointList = new List<Waypoint>();
    [SerializeField] [Range(0.1f, 100f)] float duration = 1f;
    [SerializeField] [Range(0f, 1f)] float lookAheadAmount = 0.1f;
    [ValueDropdown("_easingTypes")] public Ease pathEase;
void Start()
    {
        FindPath();
    }

    void FindPath()
    {
        waypointList.Clear();
        var pathData = GameObject.Find("Path Data Manager").GetComponent<PathData>();
        waypointList = pathData.WaypointList;
        GeneratePath(waypointList);
    }
    void GeneratePath(List<Waypoint> wpList)
    {
        var wayPositions = new Vector3[]{};
        foreach (var waypoint in wpList)
        {
            var wayPos = waypoint.transform.position;
            var endPos = new Vector3(wayPos.x, transform.position.y, wayPos.z);
           wayPositions = wayPositions.Append(endPos).ToArray();
        }

        ExecutePath(wayPositions);

    }

    void ExecutePath(Vector3[] wayPositions)
    {
        transform.DOPath(wayPositions, duration, PathType.Linear, PathMode.Full3D)
            .SetLookAt(lookAhead: lookAheadAmount, Vector3.left).SetOptions(true).SetEase(pathEase).SetLoops(-1);
    }

    static IEnumerable _easingTypes = new ValueDropdownList<Ease>()
    {
        {"Linear", Ease.Linear},
        {"OutQuad", Ease.OutQuad},
        {"InOutBounce", Ease.InOutBounce},
    };

I tried using DOTween.Kill on it's transform before destroying it but that didn't stop the error from happening.

Demigiant commented 3 years ago

Are you sure you're using DOTween.Kill on the correct transform (the one you use in trasnform.DOPath)? I would also recommend to simply store the tween generated by transform.DOPath and kill that directly inside the gameObject's OnDestroy (though that's not the problem here because the static kill with the right target already should work, unless the tween is regenerated after for some reason).

Setmaster commented 3 years ago

Putting the path.kill inside gameObject OnDestroy worked

fringe26 commented 1 year ago

@Setmaster How Did you do that? please Explain. Dotween.Kill(transform) inside of OnDestroy ofObject?

DiogoOliveiraDev commented 1 year ago

@Setmaster How Did you do that? please Explain. Dotween.Kill(transform) inside of OnDestroy ofObject?

He put path.kill() on gameObject reference that has the DOPath in the method OnDestroy() (MonoBehaviour)