I have a coroutine that should simulate movement, but I keep getting this error after the first if statement executes once:
NullReferenceException: Object reference not set to an instance of an object
TrainMover+<MoveNX>c__Iterator1.MoveNext () (at Assets/Scripts/TrainMover.cs:117)
Here is the script:
IEnumerator MoveNX()
{
if (Vector3.Distance(gameObject.transform.position, GameObject.Find("Target").transform.position) >= 0.1)
{
gameObject.transform.position -= new Vector3(0.1f, 0, 0);
yield return new WaitForSeconds(0.1f);
}
if (Vector3.Distance(gameObject.transform.position, GameObject.Find("Target").transform.position) < 0.1)
{
Destroy(GameObject.Find("Target"));
moving = false;
StopCoroutine(MoveNX());
}
}