C# Instantiate Problem

I rather new to Unity and am a little confused.

I have a spawn script attached to the main character that spawns a zombie at a random place every x seconds. The random places are actually empty game objects splattered liberally around the scene. Here are my problems:

  1. The zombie prefab instantiates in the same place every time. Here is my instantiate code:

    GameObject go = Instantiate(Resources.Load("Zombie")) as GameObject; ZombieSpawned = go.GetComponent("Zombie") as ZombieMove;

Is it possible to tell it where to spawn?

Also when the prefab spawns the script that is attached to it, a script named ZombieMove fails to have a target. Normally the script is attached to a zombie, and then I would drag the target to the script, but as its a prefab I dont think that the linked script can reference anything thats not a prefab.

Thanks for any help :)

Instead of using Resources.Load(...), I'd use a Transform as public variable into which you drag the prefab (but that's just something I personally prefer - I think it also helps Unity to know what to include in the scene).

Anyways, as you can see in the scripting reference, Instantiate also takes position and rotation, if you wish. So, what you might want to do is put the spawn-spots into an array which is also a public variable of that script in which you're spawning the zombies. Then use a random integer to select one of the entries in that array and use its position and rotation. Something like this should do (untested example code to illustrate what I explained before - use at own risk ;-) ):

public class SpawnStuff : MonoBehaviour {
    public Transform zombiePrefab = null;
    public Transform[] spawnPoints = null;

    // [some other stuff you may want to do]

    public void Awake() {
        if (zombiePrefab == null) {
            Debug.LogError("You forgot to assign a zombiePrefab");
        if (spawnPoints == null || spawnPoints.Length == 0)  {
            Debug.LogError("You forgot to set up the spawn points correctly");

    public void SpawnRandomZombie() {
        Transform spawnPoint = spawnPoints[Random.Range(0, spawnPoints.Length)];
        GameObject go = (GameObject) Instantiate(zombiePrefab, spawnPoint.position, spawnPoint.rotation);
        // [do more stuff you want to do]

If you get a null reference exception on spawnPoint.position, you also didn't set up your spawn points correctly (you have spawn points in the array which are null). You could make sure that happens by going over all of them in a foreach-loop in Awake and screaming ERROR if one of them is null.

GameObject go = (GameObject) Instantiate(zombiePrefab, spawnPoint.position, spawnPoint.rotation); Both instantiates, and sends you a reference to the zombie.

You can set the target by calling something similar to:

ZombieMove zm = (ZombieMove) go.GetComponent(typeof(ZombieMove)) as ZombieMove; zm.TheTarget = TheTarget;

Then, add a transform variable called "TheTarget" at the beginning of your script.