Instantiate with Prefabs

Hi All,
I have some code which I would like to spawn a monster. Here’s a snippet of my code:

________________________

public var prefab : PrefabType;
public var spawnPlace : GameObject;
// ........ (a lot of other stuff)
Instantiate(prefab, spawnPlace.transform.position, transform.rotation)

________________________

And then I get an error:

BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEditor.PrefabType, UnityEngine.Vector3, UnityEngine.Quaternion)' was found.

I’m guessing that’s because I’m using a variable type PrefabType rather than GameObject, but in my game my enemies only move in one direction, and I’ve made some code,

	if (transform.position.x < **A Certain Point**){
		Destroy(gameObject);
	}	

which destroys my enemies after they get off the edge of the screen, but if I Instantiate a GameObject, after a while, the “spawn object” will go past the “certain point” and no more will spawn, so that’s why I want to Instantiate a Prefab.

Does anyone know how to Instantiate a prefab? Any help would be much appreciated.

(If its not clear, I’m a beginner with Unity, and I’m trying to get my head around the Instantiate function/command/thingy.)

‘Prefab’ is something specific in Unity. It gets used on this list to mean “anything we are making copies of,” but in reality there is a specific process to make a prefab. A prefab does not live in the hierarchy, so your problem of the spawn object being destroyed will not happen with a true prefab. Here is a link that walks through a couple of prefab creation scenarios:

http://docs.unity3d.com/Documentation/Manual/InstantiatingPrefabs.html

I see what the problem is now.
Here’s how to fix your problem:

The GameObject that you are copying to make your enemies needs to be a prefab.

  • In your Assets folder, create a new
    folder called “Prefabs”.
  • Click on
    your GameObject in the scene that represents your
    enemy and drag it into the folder
    called “Prefabs”.
  • Confirm that the
    copy of your enemy is now in the
    prefabs folder. (You can test that
    it’s correct by clicking on the
    object in Prefabs folder and dragging
    it back into your scene. If it looks
    the same and has all the same
    properties as a normal enemy it
    worked).
  • Once confirmed that the
    prefab is there, you can now delete
    all enemies (even the one you were
    copying from).

You now have a scene with your level, but no enemies or enemy to copy.

Now there is a couple of ways to go from here, which will probably need a little more detail from yourself.
If the script you linked above is a game manager that spawns your mobs, and it’s attached to a GameObject in the scene then life is easy, follow these steps:

  • In the script, change the prefab type back to GameObject (from PrefabType)
  • Click on the game manager object in the scene, you should see your script attached on the right in the inspector.
  • You should see a field called “prefab”, drag your new prefab on top of this field and drop it in.

Now when the instantiate fires, it will spawn an enemy from the prefab. Since the prefab is not in your scene, it will never disappear.

If the script isn’t a game manager and attached to an object, you’ll need to find the prefab using code. To do this, one quick change is required:

In your Prefabs folder, create a new folder called “Resources” and now drag your enemy from the Prefabs folder into the Resources folder.

Now, in your code where you have:

public var prefab : PrefabType;

Delete that line.

And your instantiate line should change from:

Instantiate(prefab, spawnPlace.transform.position, transform.rotation)

to

Instantiate(Resources.Load("PrefabName"), spawnPlace.transform.position, transform.rotation)

Where “PrefabName” is the exact name of what your prefab is called within the Resources folder.

Rob.

Here’s a quick picture example of a prefab in action. Thought it might help.