how to set a variable in prefabs or use an object in the hierarchy/prefab section in my script

So, i have a spawner script and a waypoint movement script (Spawner.cs and WayPointAI.cs)

Spawner -

 using UnityEngine;
    using System.Collections;
    
    public class Spawner : MonoBehaviour 
    {
    
    	public Transform spawnPoint;
    	public Transform spawnObject;
    	public int spawnTotal;
    	public float timeBetweenSpawns;
    
    	// Use this for initialization
    	void Start () 
    	{
    		StartCoroutine (SpawnGameObject());
    	}
    	
    	// Update is called once per frame
    	void Update () 
    	{
    	
    	}
    	IEnumerator SpawnGameObject ()
    	{
    		for( var x = 0; x < spawnTotal; x++)
    	    {
    			Instantiate(spawnObject, spawnPoint.position, spawnPoint.rotation);
    			yield return new WaitForSeconds(timeBetweenSpawns);
    		}
    	}
    }

My spawner script will spawn a NPC prefab on the spawner object.

Waypoint movement script -

	// Use this for initialization
	void Start () 
	{
		//find each waypoint object in the scene when game is loaded
		wayPoint [0] = GameObject.Find("WayPoint 1").transform;

	}
	
	// Update is called once per frame
	void Update () 
	{
		// once npc reaches last waypoint, it is destroyed
		if(currentWayPoint == 1)
		{
			Destroy (this.gameObject);
		}
		else
		{
			walk();
		}

	}
	//makes npc move
	void walk()
	{
		//facing direction
		//npc looks at next waypoint
		Quaternion rotation = Quaternion.LookRotation(wayPoint[currentWayPoint].position - transform.position);
		//how fast npc turns
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime*rotationSpeed);
		
		//movement
	    Vector3 wayPointDirection = wayPoint[currentWayPoint].position - transform.position;
	    float speedElement = Vector3.Dot(wayPointDirection.normalized, transform.forward);
        float speed = accelerate * speedElement;
		transform.Translate(0,0,Time.deltaTime*speed);
		}

	void OnTriggerEnter(Collider collider)
	{
		if (collider.tag == "WayPoint 1") 
			Destroy (this.gameObject);
			//currentWayPoint = 1;
			//currentWayPoint++;
	}

}

After Spawner.cs pulls an NPC from the prefabs and then spawns it on the Spawner object, WayPointAI will move the NPC fromt he spawner, to WayPoint 1 (object in the hierarchy). once the NPC reaches WayPoint 1, it despawns (is destroyed) the problem here is that i can’t set the WayPoint element 0 to an object when it’s in the prefab folder (when i drag the WayPoint1 object from the hierarchy onto the WayPoint element 0 variable in the NPC’s WayPointAI script (the npc is in the hierarchy), i get an error sign and it doesn’t let me set the variable to the object in the hierarchy. you may think “why is this a problem? the NPC is set to walk to an object named ‘WayPoint 1’, so why do you need to set the variable to the WayPoint 1 object too?”. That’s a good question. the reason i want to be able to set the variable is because i want to make multiple spawners that go to their own WayPoint 1’s. so there’s two ways i can solve this problem: first, i could make it so i can set the variable on the NPC’s WayPointAI script in the prefabs folder and everything will be golden, or i can waste time and space by making another 50 identical scripts with the two lines with “WayPoint 1” in them to “WayPoint 2” and “WayPoint 3”, and so on. but i would really prefer it if i could just set a variable on the NPC in the prefab folder. does anyone know how? help would be greatly appreciated! and if you’re confused on soemthing just ask me and i’ll try to be more specific and detailed.

Thanks in advance!

Put the waypoint(s) on the spawner, and drag and drop them from the hireachy.

Then, when you spawn a NPC, pass the waypoints to it:

for( var x = 0; x < spawnTotal; x++)
{
    GameObject spawned = Instantiate(spawnObject, spawnPoint.position, spawnPoint.rotation) as GameObject;
    WaypointMovement mover = spawned.GetComponent<WaypointMovement>();
    mover.waypoints = waypoints;
    yield return new WaitForSeconds(timeBetweenSpawns);
}

You should also try to get rid of all string comparrison and GameObject.Find - they’re incredibly inflexible, as any tiny misspelling (like an extra space) will break your code. GameObject.Find is also really slow.

so my spawner currently looks like this -

using UnityEngine;
using System.Collections;

public class Spawner : MonoBehaviour 
{

	public Transform spawnPoint;
	public Transform spawnObject;
	public int spawnTotal;
	public float timeBetweenSpawns;
	private WayPointAI wayPointAI;
	public Transform[] waypoint;

	// Use this for initialization
	void Start () 
	{
		StartCoroutine (SpawnGameObject());
	}
	
	// Update is called once per frame
	void Update () 
	{
	
	}
	IEnumerator SpawnGameObject ()
	{
		for( var x = 0; x < spawnTotal; x++)
	    {
			GameObject spawned = Instantiate(spawnObject, spawnPoint.position, spawnPoint.rotation) as GameObject;
			WayPointAI walk = spawned.GetComponent<WayPointAI>();
			walk.wayPoint = waypoint;
			yield return new WaitForSeconds(timeBetweenSpawns);
		}
	}
}

in my hierarchy i have Spawner’s waypoint variable set to the waypoint, (array size of 1) and Waypoint movement’s waypoint variable is set to the waypoint as well (also with an array size of 1). everything runs fine but when i run it, it will spawn in one NPC that doesn’t move and i’ll get the error “NullReferenceException: Object not set to an instance of an object, Spawner+c__Iterator0.MoveNext ()”

and it will point me to this line of code -

WayPointAI walk = spawned.GetComponent<WayPointAI>();