How to pass arrays from one object to another?

The arrays are initialized in the inspector as shown below which is attached to the same object (“A”) that the “Pathing” script is attached to.

IMAGE HERE

The script (“Pathing”) has a function below:

	public Transform[] getPath(){
		return path;
}

I call the above function in the following script (“PathFollower”) which is attached to another gameObject (“B”):

void Start () {
		path=other.getPath();
	}

However it says the following 2 errors:

NullReferenceException: Object reference not set to an instance of an object
Pathfollower.Start () (at Assets/Pathfollower.cs:16)

How can I get the array from the other gameObject? I would assume that the code will work cos the array for “A” will be initialized at Start and I will be able to pass on the array to “B” to work on. Thanks in advance for any advice!

if you do not use code to grab a reference to Pathing for the variable other, show it in the inspector and assign it the Pathing script from object A.

It’s hard to tell without posting your entire script for Pathing and PathFollower. Displaying the lines for each error (along with the script) helps us better understand the problem; so I’m assuming the errors in question pertain to the code posted above.

Question: How is the variable other declared. Is it a reference to the Pathing script, a GameObject type, another type: int, float, string, etc.? Without the full code we can’t tell what it is.

If you know what GameObject you need to grab the array from, simply declare the variable other as a public reference to the Pathing script and drag-and-drop the GameObject inside the inspector, like so:

PathFollower script

using UnityEngine;
using System.Collections;

public class PathFollower : MonoBehaviour {

	public Pathing     other;
	public Transform[] path;

	void Start () {
		path = other.getPath();
	}
}

The variable other will appear inside the inspector on the GameObject using the PathFollower script. As previously stated, just drag-and-drop the GameObject using the Pathing script onto the variable other.

I’ve just tested this myself and it works. Hope this helps!