Spawning enemy mobs .......... Instantiate woe ?

Hi guys, my second question here.

“Spawning enemy mobs. the enemy works fine by itself (as a prefab) but not when it’s spawned using this code (instantiate)?”

I have an enemy, which, if I use by itself, works 100%. When I say use by itself, I mean dragging and dropping it on to the game world. However, I wish to instantiate it using the below code, to spawn it whenever the player enters. Now, it does spawn the enemy I want but a major problem occured.

The player is unable to attack the instantiated enemy. the instantiated enemy is working well on it’s side, it chases the player and can apply damage. The player can only do it to the normal enemies, not the ones instantiated. Below are two codes. One code is in Javascript, from the lerpz tutorial. It works well in spawning enemies but I am unfamiliar with javascript compared to c#. If it helps, all my other codes are in C#. The one below the lerpz code is my own code. It’s mainly for testing purposes.

Both codes spawn enemies well and the enemies interact well with the player. Neither codes allows the player to interact with the enemy. My attack code for player works 100% otherwise and can be found here via the selected answer.

Lerpz enemy respawn code:

	This script checks if the player is in range. If so, it instantiates the enemy prefab specified. When the player moves out of range, the prefab is automatically destroyed.
	This prevents repeated calls to the enemy's AI scripts when the AI is nowhere near the player, this improving performance.


var spawnRange = 0.0;	// the distance within which the enemy should be active.
var gizmoName : String;		// the type of the object. (See OnDrawGizmos() for more.)
var park : GameObject;	// link to the Prefab we'll be instantiating / destroying on demand.

// Cache variables, used to speed up the code.
private var player : Transform;
private var currentEnemy : GameObject;
private var wasOutside = true;

// Called on Scene startup. Cache a link to the Player object.
// (Uses the tagging system to locate him.)
function Start ()
	player = GameObject.FindWithTag("Player").transform;

// Called at least once every game cycle. This is where the fun stuff happens.
function Update ()
	// how far away is the player?
	var distanceToPlayer = Vector3.Distance(transform.position, player.position);

	// is he in range?
	if (distanceToPlayer < spawnRange)
		// in range. Do we have an active enemy and the player has just come into range, instantiate the prefab at our location. 
		if (!currentEnemy && wasOutside)
			currentEnemy = Instantiate(park, transform.position, transform.rotation);
		// player is now inside our range, so set the flag to prevent repeatedly instantiating the prefab.
		wasOutside = false;
	// player is out of range.
		// is player leaving the sphere of influence while our prefab is active?
		if (currentEnemy && !wasOutside)
			Destroy(currentEnemy);	// kill the prefab...

		// ...and set our flag so we re-instantiate the prefab if the player returns.
		wasOutside = true;

// Called by the Unity Editor GUI every update cycle.
// Draws an icon at our transform's location. The icon's filename is derived from the "type" variable, which allows this script to be used for any enemy.
function OnDrawGizmos ()
	Gizmos.color = Color(1, 1, 1, 1);
	// See the help docs for info on where the icon needs to be stored for this function to work.
	Gizmos.DrawIcon(transform.position, gizmoName + ".psd");	

// Called by the Unity Editor GUI every update cycle, but only when the object is selected.
// Draws a sphere showing spawnRange's setting visually.
function OnDrawGizmosSelected ()
	Gizmos.color = Color(0, 1, 1);
	Gizmos.DrawWireSphere(transform.position, spawnRange);

my code for testing:
using UnityEngine;
using System.Collections;

public class respawn : MonoBehaviour {

public GameObject target;
public GameObject target2;
	// Use this for initialization
	void Start () {

	// Update is called once per frame
	void Update () {
	float distance = Vector3.Distance(target.transform.position, transform.position);

		if(Input.GetKeyUp(KeyCode.Z)) {
           Instantiate(target2 as GameObject);



any help is very much appreciated. It’s quite confusing when the prefab itself works but when I instantiate it as above, the player stops recognizing the very same prefab.

The script on the prefab doesn’t save any variables that are assigned to an object in the scene though the inspector.

I suspect your code has little to do with it. How is the player supposed to damage the enemy? (This is the problem, right?) My guess is it’s caused by collisions or triggers, and chances are the instantiation is creating something in the wrong layer and is being ignored by the collision detection code. Or perhaps your prefab is missing the collider.