Went through the loop but didn't show enemy on screen

So mine would go through the loops (using Debug.Log to check) but my enemy wouldn’t show until I changed the code from else to if (waypointIndex == waypoints.Count).

Why didn’t it work before?

    //else
    if (waypointIndex == waypoints.Count)
    {
            Debug.Log("3");
        Destroy(gameObject);
    }

Thanks

The whole script:

public class EnemyPathing : MonoBehaviour
{
[SerializeField] List waypoints;
[SerializeField] float moveSpeed = 2f; //2

int waypointIndex = 0; //1
//only looking for the transform information from the waypoints


void Start()
{
    transform.position = waypoints[waypointIndex].transform.position; //3 4
}

void Update()
{
    Move();
}

private void Move()
{
    if (waypointIndex < waypoints.Count) //5
    {
        Debug.Log("1");
        var targetPosition = waypoints[waypointIndex].transform.position;  //6 7
        var movementThisFrame = moveSpeed * Time.deltaTime;
        transform.position = Vector2.MoveTowards //8
            (transform.position, targetPosition, movementThisFrame);

        if (transform.position == targetPosition) //9
        {
            Debug.Log("2");
            waypointIndex++;
        }
    //else
    if (waypointIndex == waypoints.Count)
    {
            Debug.Log("3");
        Destroy(gameObject);
    }
    }
}

I had my else statement inside my if statement. When I pulled it out it worked.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms