Every time I relaunch my game I Object reference not set to an instance of an object

My AI script always gets a error when I relaunch it or build it
I have to delete the way point script on my nodes and add them back for it to work
I don’t know why this happens.
Waypoints Script:
using UnityEngine;

public class Waypoints : MonoBehaviour {

	public static Transform[] points;

	void Start ()
	{
		points = new Transform[transform.childCount];
		for (int i = 0; i < points.Length; i++)
		{
			points *= transform.GetChild(i);*
  •  }*
    
  • }*

}
Enemy Script
using System;
using UnityEngine;

public class Enemy : MonoBehaviour {
public GameObject deathEffect;
public GameObject BeingShot;
public float speed = 10f;
public int health = 500;
public Transform enemyObj;

  • private Transform target;*

  • private int wavepointIndex = 0;*

  • void Start ()*

  • {*

  •  target = Waypoints.points[0];*
    

}

  • void Update ()*
  • {*
  •  Vector3 dir = target.position - enemyObj.position;*
    

_ transform.Translate(dir.normalized * speed * Time.deltaTime, Space.World);_

  •  if (Vector3.Distance(enemyObj.position, target.position) <= 50f)*
    
  •  {*
    
  •  	GetNextWaypoint();*
    
  •  }*
    
  • }*

public void takeDamage(int ammount)
{
GameObject effect = (GameObject)Instantiate(BeingShot, transform.position, Quaternion.identity);
Destroy(effect, .3f);
Debug.Log(“Has taken damage”);
health -= ammount;

if (health <= 0)
{

death();

}

}

void death()
{
GameObject effect = (GameObject)Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(effect, 5f);
Destroy(gameObject);
}

void GetNextWaypoint()

  • {*
  •  if (wavepointIndex >= Waypoints.points.Length - 1)*
    
  •  {*
    

endpath();

  •  	return;*
    
  •  }*
    
  •  wavepointIndex++;*
    
  •  target = Waypoints.points[wavepointIndex];*
    
  • }*

public void endpath()
{
Playerstats.Lives–;
Debug.Log(“U should work :/”);
Destroy(gameObject);
}
}

Hi @lonelycamper ,

That looks to me a race condition. If the AI scripts execute before the Waypoints you’ll hit that issue. The easiest solution is to initialize the Waypoint script in the Awake:

 public class Waypoints : MonoBehaviour {
 
     public static Transform[] points;
 
     void Awake()
     {
         points = new Transform[transform.childCount];
         for (int i = 0; i < points.Length; i++)
         {
             points *= transform.GetChild(i);*

}
}
}
This way you won’t hit that problem, and the children transform are there even if they are not initialized yet, so you can safely retrieve their reference.

Try lazy load it:

    private Transform[] _points;
    public static Transform[] points
    {
        get
        {
            if (_points != null) return _points;
            _points = new Transform[transform.childCount];
            for (int i = 0; i < _points.Length; i++)
            {
                _points *= transform.GetChild(i);*

}
return _points;
}
}
Could be that you have to reference the MonoBehaviour before actually not sure
Waypoints waypoints = GetComponent();
if the waypoints script is also on the enemy. Then you can use it like waypoints.points[0]