-1

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**
}
David
  • 3,756
  • 12
  • 45
  • 77
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – René Vogt Mar 20 '16 at 16:15
  • I'm sure that there is an exception with your second attempt, too. `waypoints` has never been initialized and therefor is `null`. See http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it?s=1|8.8322 – René Vogt Mar 20 '16 at 16:17
  • I'm working with Unity. waypoints are OK, they DO exist. If use Vector3 startPosition = wp.waypoints[0].transform.position; - code is WORKING – David Mar 20 '16 at 16:22
  • Look at my answer. array is the problem. you must use the new key. – Programmer Mar 20 '16 at 16:32

4 Answers4

2

Look very close at this

public class WaypointsClass
{
    public GameObject[] waypoints;
}

WaypointsClass.waypoints is an ARRAY! You must use the new keyword to create array or stuff like that will happen. wp.waypoints = new GameObject[waypoints.Length];

It should look like this

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]; //This line you missed
        wp.waypoints = waypoints;
        Vector3 startPosition = wp.waypoints[currentWaypoint].transform.position;

EDIT:

From your comment, you can re-use WaypointsClass wp = new WaypointsClass(); by putting WaypointsClass wp outside the Start function then initialize it in the Start function like below:

WaypointsClass wp = null; //Outside (Can be used from other functions)
void Start () {
            wp = new WaypointsClass(); //Init
            wp.waypoints = new GameObject[waypoints.Length]; //This line you missed
            wp.waypoints = waypoints;
            Vector3 startPosition = wp.waypoints[currentWaypoint].transform.position;
}
Programmer
  • 104,912
  • 16
  • 182
  • 271
  • Your code is working , but if II use in Update () { Vector3 startPosition = wp.waypoints[currentWaypoint].transform.position; } It fails. Does it mean I must create this thing each time I use Update ??? I thought I must do it only once – David Mar 20 '16 at 16:37
  • You did WaypointsClass wp = new WaypointsClass(); which is a new instance but you must do thesame for the array. This should solve your problem but post your update code. – Programmer Mar 20 '16 at 16:39
  • I made an edit. You can re-use it by placing it outside the Start function and then initializing it in the start function. Then you can use it anywhere else. Look at the edit. – Programmer Mar 20 '16 at 16:42
0

You need to construct the GameObject in the class WaypointsClass otherwise you will get a null pointer exception...

and if you do this:

wp.waypoints = waypoints;

and the waypoints of the right side is a null reference the you will get a NPE too.

ΦXocę 웃 Пepeúpa ツ
  • 43,054
  • 16
  • 58
  • 83
0

You haven't initialized the waypoints collection in MoveEnemy - you're attempting to assign null to wp.waypoints (I assume you're receiving a NullReferenceException...).

tmthydvnprt
  • 8,832
  • 7
  • 49
  • 66
  • Vector3 startPosition = wp.waypoints[0].transform.position; is WORKING Vector3 startPosition = wp.waypoints[currentWaypoint].transform.position; Is NOT working. – David Mar 20 '16 at 16:26
0

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.

  • The array waypoints is not empty, it's created from unity Vector3 startPosition = wp.waypoints[0].transform.position; is WORKING – David Mar 20 '16 at 16:27
  • I believe Programmer's reply is the correct one, my apologies! let us know if his solution works. – CyberCasper Mar 20 '16 at 16:40
  • @CyberCasper OP is using the access modifier **public** on his gameobjects of arrays, so we should assume that he's setting them in the inspector which is what one normally does in unity. – Martin Dawson Mar 20 '16 at 16:55