Spawn prefab and move toward another gameObject and code organisation

I’m a complete beginner at unity so please bear with me. I have searched a lot and found some answers but I’m not fully understanding things.

The objective I want to achieve is to spawn a new AI character on the map. I then want that AI character to move toward a building on the map. However there could be multiple spawns of these AI characters and several other buildings I want them to move to independently to each other.

I have a couple of issues really. First, is the following a correct way to organise the code:

  • I have an empty gameObject next to
    the first building as a spawn
    location for the AI. A script is also
    attached to this empty gameObject to
    control the spawning of the AI.
  • I have a second empty gameObject next
    to another building for the AI to
    move toward.
  • I have a prefab character as the AI
    with a script attached. This script
    will control the AI movement and
    behaviours etc.

One issue here, is that I may also want the AI to spawn at a different building, so creating lots of empty gameObjects with scripts for spawning this AI on the other buildings seems like a stupid way to do it?

My other issue is that there is a disconnect between the spawn of the AI and the movement. So I am struggling to get the AI character to move to another location after spawning and not really understanding how to trigger this kind of action outside of the Update() function.

Sorry if this seems really dumb but any kind of advice would be appreciated.

Here is the spawn script:

#pragma strict

var spawnLocations : Transform[];
var aiPrefab : GameObject;
var aiPrefabClone : GameObject;

function Start(){
	spawnAi();
}

function spawnAi(){
	aiPrefabClone = Instantiate(aiPrefab,spawnLocations[0].transform.position,Quaternion.Euler(0,0,0)) as GameObject;
	aiPrefabClone.goToMine(spawnLocations[1].transform.location);
}

Here is the script attached to the AI Prefab:

#pragma strict

function Start () {
}

function Update () {
}

function goToMine(location){
	transform.position = Vector3.Lerp(transform.position, location, Time.time);
}

This is currently throwing an error:
BCE0019: ‘goToMine’ is not a member of ‘UnityEngine.GameObject’.

Thanks

You could do it like this:

Have one script on a persistent gameobject (a “manager” type of GameObject) that controls the spawning of the units (as I will call them).

This “spawn control” script weill have an array or list or Vector3s that serve as possible spawn positions. This eliminates the need to have multiple gameobjects in the scene to act as spawn points). If you have trouble entering the vector3 coordinates, you could quickly create a gameobject at the desired position next ot a building, copy it’s transform xyz values, and use those to add a new Vector3 to your array/list, and then delete the gameobject. This way, you can visualize the spawn position to make sure it’s correct.
You could of course just keep your Transform array if it suits you.

In your spawn control script, once a unit is spawned (instantiated), access it’s movement/AI script and assign one of the target positions to it (you could use one of the spawn positions or have another collection of positions for that).

About your error: You try to access a function “goToMine” on the GameObject you just instantiated, but of course a GO doesn’t have such a function. What you really want is to access this function on the Movement/AI script that is attached to the instantiated GO, so you GetComponent:

aiPrefabClone.GetComponent<MovementScript>().goToMine(spawnLocations[1].transform.location);

“MovementScript” of course has to be replaced by whatever your actual scriupt is named.

To make the unit move at another time, you either have to keep a reference to each unit in your control script (array, list or any other collection type) and make sure it is properly maintained (adding a unit when one is spawned, removing it when one is destroyed), or pass the movement target position to the Movement script but not do anything with this position in the Movement script until you want to.
A third, but less efficient option, is to use Object.FindObjectsOfType
to get all the currently existing Movement scripts in the game at any time and find the right one.