#stopcoroutine not working

1 messages · Page 1 of 1 (latest)

desert glen
#

Objects still following the player after collision even though i am stopping the coroutine

#
    void Start()
    {
        stacking = FindObjectOfType<Stacking>();
    }
    public void UpdateCubePosition(Transform followedCube, bool isFollowStart)
    {
        StartCoroutine(StartFollowingToLastCubePosition(followedCube, isFollowStart));
    }

    IEnumerator StartFollowingToLastCubePosition(Transform followedCube, bool isFollowStart)
    {

        while (isFollowStart)
        {
            yield return new WaitForEndOfFrame();
            transform.position = new Vector3(Mathf.Lerp(transform.position.x, followedCube.position.x, followSpeed * Time.deltaTime),
                transform.position.y,
                Mathf.Lerp(transform.position.z, followedCube.position.z, followSpeed * Time.deltaTime));
        }
    }

    void Update()
    {
        if(stacking.canStopFollow == true)
        {
            StopAllCoroutines();
        }
    }```
This is the script
fossil schooner
desert glen
#

coroutine is called when the player collides with the scope, And canstopfollow is true when he collides with an enemy.

    private void OnTriggerEnter(Collider col)
    {
        if (col.CompareTag("Scope"))
        {
            _cubeList.Add(col.gameObject);
            if (_cubeList.Count==1)
            {
                _firstCubePos = GetComponent<MeshRenderer>().bounds.max;
                _currentCubePos = new Vector3(transform.position.x, _firstCubePos.y, transform.position.z);
                col.gameObject.transform.position = _currentCubePos;
                _currentCubePos = new Vector3(transform.position.x, transform.position.y + 0.1f, transform.position.z);
                col.gameObject.GetComponent<Scopes>().UpdateCubePosition(transform, true);
                //col.transform.SetParent(transform.parent);
                //col.transform.rotation = transform.parent.rotation;
            }
            else if (_cubeList.Count > 1)
            {
                col.gameObject.transform.position = _currentCubePos;
                _currentCubePos = new Vector3(transform.position.x, col.gameObject.transform.position.y + 0.1f, transform.position.z);
                col.gameObject.GetComponent<Scopes>().UpdateCubePosition(_cubeList[_cubeListIndexCounter].transform, true);
                //col.transform.SetParent(transform.parent);
                //col.transform.rotation = transform.parent.rotation;
                _cubeListIndexCounter++;
            }
        }
        // else if(col.gameObject.CompareTag("Enemy"))
        // {
        //     _cubeList.Clear();
        //     canStopFollow = true;
        // }
    }```