Adding waypoints to a prefab

Hello, I am in a bit of a kerfuffle with this. I currently have an object that is being spawned and would like it to follow a specific set of waypoints. As it stands I can spawn the object with waypoints filled in as long as the original object is still in the scene. However, if I try to place a prefab into the spawner, the array is empty, thus causing an error.

How would I go about filling the array for the prefab once it has been spawned? I assume I could make a script that holds the waypoints and somehow get the prefab to get the points but how could this be done?

This is essentially the script that controls the prefab along with the waypoints:

 public Transform[] wayPoints;
	public float speed = 5.0f;
	public float rotSpeed = 5.0f;
	private int currentWaypoint = 0;
	
	private CharacterController character;
	private Vector3 target;
	private Vector3 moveDirection;
	
	private Quaternion _lookRotation;
        private Vector3 _direction;

void Start ()
	{
    	character = GetComponent<CharacterController>();
	}
 
	void Update () 
	{
    	        if(curHealth >=1)
		{
			Move ();
		}
                else
                      Die ();
	}


        void Move()
	{
		if(currentWaypoint < wayPoints.Length)
    	{
        	target = wayPoints[currentWaypoint].position;
        	target.y = transform.position.y; // keep waypoint at character's height
        	moveDirection = target - transform.position;
			
        	if(moveDirection.magnitude < 1)
        	{
            	transform.position = target; // force character to waypoint position
        	}
			else
			{
				_direction = (target - transform.position).normalized;
				_lookRotation = Quaternion.LookRotation(_direction);
				
				transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * rotSpeed);
        		character.Move(moveDirection.normalized * speed * Time.deltaTime);
			}
    	}
	}
	
	void Die()
	{
		Debug.Log("DEAD");
		Destroy(gameObject,1.0f);
	}
	
	
	void OnTriggerEnter(Collider other)
	{
		currentWaypoint++;		
	}
}

Cheers

I’d suggest creating GameManager Singleton instance for your scene, that holds a reference to any waypoints.
Create an empty gameobject in your scene called GameManager, and attach the GameManager script to it.
Use this class for all scene specific stuff that all gameobjects in your scene can refer to.

Something like this…

----- GameManager.cs ----
using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour 
{    
    List<Transform> _wayPoints;
    public List<Transform> Waypoints { get { return _wayPoints; } set { _wayPoints = value; } }

    static GameManager _instance;
    public static GameManager Instance
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType(typeof(GameManager)) as GameManager;
            }
            return _instance;
        }
        set { _instance = value; }
    }

    void Awake()
    {
        Instance = this;       
     }

     void Start
     {
        // Initialise waypoints here
        // _waypoints = 
     }
}

Then when your creature spawns, get the waypoints from this GameManager object.

i.e

--- Creature.cs
void Start()
{
  waypoints = GameManager.Instance.Waypoints;
}

Cheers dude, that worked a treat. :slight_smile:

The only thing I don’t really get is how to initialise the waypoints in the start function of the GameManager. I have fixed waypoints anyway so it isn’t too much work to put them in the list but if there is a way to do that programmatically that would obviously save me some time.

Well, I assume your waypoints are basically a set of transforms right?
There are several ways to do this…

Create a waypoint parent object, and have all the waypoints underneath it.
i.e

WaypointSet1

  • Waypoint1
  • Waypoint2

etc…

Then in your start method, just get a reference to WaypointSet1, then loop through all it’s child transforms, and add each transform to your _wayPoints List variable…