can't get component Transform

Hello everybody. I have one problem, I can’t get the component Transform in the loop. Always get an error
Why I do this I have an enemy and he has to follow a given route, and in order not to set the object manually I want to get it in the script. and the strangest thing is that if you do it not in a cycle, then everything works fine. I can’t solve the problem in any way

public class Test : MonoBehaviour
{
    [SerializeField] private int speed;
    [SerializeField] private int enemyPointTransform;
    private Transform[] enemyNextTransform;

    private GameObject road;

    void Start()
    {
        enemyPointTransform = 0;
        road = GameObject.FindGameObjectWithTag("Road");

        for (int i = 0; i < road.transform.childCount; i++)
        {
            enemyNextTransform[i] = road.transform.GetChild(i).GetComponent<Transform>();
        }
    }

    void Update()
    {
        transform.position = Vector3.MoveTowards(transform.position, enemyNextTransform[enemyPointTransform].position, speed * Time.deltaTime);

        if (transform.position == enemyNextTransform[enemyPointTransform].position)
        {
            enemyPointTransform++;
        }
    }
}

You get “an error?” That isn’t especially useful.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

Also, some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

thank for help =)

i forgot to initialization the array enemyNextTransform :sweat_smile: