UPDATE
public class WaypointsClass
{
public GameObject[] waypoints;
}
public class MoveEnemy : MonoBehaviour {
public GameObject[] waypoints;
private WaypointsClass wp;
private int currentWaypoint = 0;
void Start () {
WaypointsClass wp = new WaypointsClass();
wp.waypoints = new GameObject[waypoints.Length];
wp.waypoints = waypoints;
print( wp.waypoints[currentWaypoint].transform.position); **WORKING**
}
void Update () {
print(wp.waypoints[currentWaypoint].transform.position); **NOT WORKING**
}
You haven't initialized the waypoints collection in
MoveEnemy
- you're attempting to assignnull
towp.waypoints
(I assume you're receiving aNullReferenceException
...).You need to construct the GameObject in the class WaypointsClass otherwise you will get a null pointer exception...
and if you do this:
and the waypoints of the right side is a null reference the you will get a NPE too.
Look very close at this
WaypointsClass.waypoints
is an ARRAY! You must use thenew
keyword to create array or stuff like that will happen.wp.waypoints = new GameObject[waypoints.Length];
It should look like this
EDIT:
From your comment, you can re-use WaypointsClass
wp
= new WaypointsClass(); by putting WaypointsClass wp outside theStart
function then initialize it in theStart
function like below:To try and elaborate a little in case it's not immediately apparent, you've never created any GameObject; the array waypoints is still empty, so when you try to call waypoints[0] (the value of currentWaypoint), the return is null; hence your (assumed) error: "NullReferenceException".
To resolve this, populate your array! Make a whole heap of GameObject and assign them positions.