'pathfinding tower defense NullReferenceException [duplicate]
i have done some code to allow my capsule to move from waypoint to waypoint and unity isn't picking up that exists enter image description here
and here is the code my emptybody is called WayPoints all children are called Waypoint through to Waypoint(13)
public class WayPoints : MonoBehaviour
{
public static Transform[] points;
void Awake ()
{
points = new Transform[transform.childCount];
for (int i = 0; i < points.Length; i++)
{
points[i] = transform.GetChild(i);
}
}
}
Edit: after changing to list is now coming up with error enter image description here
new code:
public class Enemy : MonoBehaviour
{
public float speed = 10f;
public static List<Vector3[]> points = new List<Vector3>();
private Transform target;
private int wavepointIndex = 0;
void start ()
{
target = WayPoints.points[0];
}
void Awake ()
{
points = new List<Vector3>();
for (int i = 0; i < transform.childCount; i++)
{
points[i] = transform.GetChild(i).position;
}
}
}
Solution 1:[1]
I don't have access to Unity to test but reading your code I would alter to work with an array or list of Vector3 objects.
Something along the lines of (not tested)
public static List<Vector3[]> points = new List<Vector3>();
void Awake ()
{
points = new List<Vector3>();
for (int i = 0; i < transform.childCount; i++)
{
points[i] = transform.GetChild(i).position;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pi and Mash |
