[Solved] Access Array from another object or script

I am trying to access a Transform array from a scene object to give to my prefab. I searched the forums and found a solution for a single variable. I tried to use what they had to work with what I need but couldn’t get it to work. Here is the script called “MinionSpawn” that is attached to the scene object. I attach the Transform points in Unity.

MinionSpawn.cs

public Transform[] waypoints;

Here is the script called “Minions” that I am trying to use the array in. The array is 14 waypoints that each instantiate minion prefab will use to navigate the map.

Minions.cs

public class Minions : MonoBehaviour {
    int currentWaypoint = 0;
    public float minionSpeed = 20;
    private MinionSpawn minionSpawn = FindObjectOfType(typeof(MinionSpawn));
    

	// Use this for initialization
	void Start () 
    {
        
	}
	
	// Update is called once per frame
	void Update () 
    {
        Transform[] waypoints = minionSpawn.waypoints;
        float amtToMove = minionSpeed * Time.deltaTime;
        if (currentWaypoint < waypoints.Length)
        {
            Vector3 target = waypoints[currentWaypoint].position;
            Vector3 moveDirection = target - transform.position;
            transform.LookAt(target);
            transform.Translate(Vector3.forward * amtToMove);

            if (moveDirection.magnitude < 1)
            {
                currentWaypoint++;
            }
        }
        else
        {
            transform.Translate(new Vector3(0,0,0));
        }
	}
}

In its current state I am getting the following error: Cannot implicitly convert type ‘UnityEngine.Object’ to ‘MinionSpawn’. An explicit conversion exists (are you missing a cast?)

In C#, you have to explicitly cast from object to MinionSpawn. FindObjectOfType returns an object which doesn’t have a definition for waypoint.
So, add change your code to this.

private MinionSpawn minionSpawn = FindObjectOfType(typeof(MinionSpawn)) as MinionSpawn;

Thanks fixed that part. Now it is saying that waypoints is null but I have the array setup in Unity. I used

print(waypoints);

to find out that the array was set to null.

Error:“System.NullReferenceException: object reference not set to an instance of an object”

Your code looks ok when I glance over it. The only thing I would try to do differently is put the FindObjectOfType in the Start().

You’re my hero! That fixed it. Thanks