- Home /
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: /* EnemyRespawn.js
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.
else
{
// 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(distance<20){
if(Input.GetKeyUp(KeyCode.Z)) {
Instantiate(target2 as GameObject);
}
}
if(distance>100)
Destroy(target2);
}
}
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.
Celsius, you may have to learn how to have a pool of objects in video gamed development. You'll have to learn this eventually so it may be time!
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
as a broad general rule, you never Instantiate anything during gameplay in a vid game. Hope the link helps!
Answer by podperson · Oct 29, 2012 at 12:47 AM
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.
could you please explain the part about the prefab not having any variables that are assigned to an object in the scene through the inspector?
because I do not have nor did I though I need that. I'm very much a beginner in unity.
Answer by TheDarkVoid · Oct 29, 2012 at 12:42 AM
The script on the prefab doesn't save any variables that are assigned to an object in the scene though the inspector.
Your answer
![](https://koobas.hobune.stream/wayback/20220613083020im_/https://answers.unity.com/themes/thub/images/avi.jpg)