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!