My Version of EnemyMovemnt

Hi everyone,

i was taking the Step of Introducing in coroutines. And when Ben give us the Task i was moving on and create my own Script to handle Exceptions too. It endet up that my Script is way Bigger then @ben ´s solution.
Is it worth in this case to handle Exceptions? Is it a good Training?

Here is my Code of the EnemyMovement:

using System.Linq; // We call the Function Last inside here
using System.Collections; // You need this for the IEnumerator
using System.Collections.Generic; //Here we Find the List
using UnityEngine;

public class EnemyMovement : MonoBehaviour {

    public List<Waypoint> path;

	void Start ()
    {
        StartCoroutine(FollowPath()); // Start Coroutine
        Debug.Log("Start Function");
    }

    IEnumerator FollowPath()
    {
        if (path != null) //We Ask if there is a path
        {
            Debug.Log("Coroutine Call");
            var lastItem = path.Last(); //Get the Last Item out of the List u need "using System.Linq" to use that Function

            foreach (Waypoint waypoint in path) //Simple ForEach
            {                
                if (waypoint != lastItem) //Check if we at the Last Item 
                {
                    Debug.Log("Waypoint: " + waypoint.name);
                    transform.position = waypoint.transform.position;
                    yield return new WaitForSeconds(1f);
                }
                else if (waypoint == lastItem) //If we end we make the Last Move and then Stop the Coroutine
                {
                    Debug.Log("Waypoint: " + waypoint.name);
                    transform.position = waypoint.transform.position;
                    Debug.Log("Stop Coroutine");
                    StopCoroutine(FollowPath());
                }
                else //If something went wrong
                {
                    Debug.Log("Something went wrong");
                    StopCoroutine(FollowPath());
                }
            }
        }
        else
        {
            Debug.Log("No Path in Waypoints");
            StopCoroutine(FollowPath());
        }
    }
} 

What you guys think about? Or would it better if i work out for Exceptions with try and catch?

Privacy & Terms