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());
}
}
The line of error is the if (Vector3.Distance(gameObject.transform.position, GameObject.Find("Target").transform.position) < 0.1) and about the "Target" GameObject, you would be right, but as previously mentioned, "Target" is periodically destroyed Destroy(GameObject.Find("Target")); and then created again (in a different part of the script).
– matthrewpJust figured it out. The problem was earlier in the script. Whoops.
– matthrewp@mika123 Thank you soooooooo much! You're awesome! :D
– Mativve