- Home /
Trying to spawn enemies
This is the code i have my enemies, what i want to do is spawn another enemy when i kill the original, this works to an extent, only it stops the enemy completely and the enemy wont go after the player. But as well as that, it seems to keep cloning the enemies everytime i hit one without destroying the other. My hierarchy just looks like enemy(Clone)(Clone)(Clone) etc.
var enemy : GameObject;
var seenDistance = 40;
var rotationSpeed = 100;
var moveSpeed = 5;
var nextSpawn : float = 0.0f;
var spawnTime : float = 5.0f;
function Start ()
{
nextSpawn = Time.time + spawnTime;
}
function Update ()
{
var distance = Vector3.Distance(transform.position, player.transform.position);
if(distance < seenDistance)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(player.transform.position - transform.position),rotationSpeed * Time.deltaTime);
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}
function spawnEnemy()
{
Instantiate(enemy, transform.position, Quaternion.identity);
}
function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.tag == "bullet")
{
Destroy(theCollision.gameObject);
if(Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnTime;
spawnEnemy();
}
}
}
Below is the destroy code i have for my enemy which is in my bulletCollide script:
function OnCollisionEnter(collision : Collision)
{
Destroy (gameObject);
Debug.Log("Hit");
if (collision.gameObject.name == "Test" ||collision.gameObject.name == "StupidEnemy")
{
Destroy(collision.gameObject);
Inventory.inventory[2] += 10;
}
}
Can someone tell me what it is im doing wrong? Any help would be appreciated.
the clone of clone can be caused because you didn't linked the object to a prefab just a gameObject in your hierarhy, so it works till you kill the original?
To fix this just create a prefab from your enemy, by draging it to the project folder and then drag the one in the project folder onto your script.
yes a agree with gajdot, just make your enemy
variable public and then drag the prefab on it in the inspector
I've made it public and the enemy was a prefab so unfortunately it didn't fix my problem, thanks for the response though.
Answer by Xelareip · Nov 21, 2013 at 05:03 PM
If this script is on the prefab, that means the script is referencing its own GameObject. Because all links intern to a prefab stay intern to the GameObject when you create an instance, whenever you instantiate a new enemy, the variable in your script won't reference the prefab but the current GameObject.
To fix that you need the variable referenced by var enemy : GameObject to be located on a script that is not part of the prefab.
Not sure that I'm clear. Shorter version : If the script that spawns the enemy is attached to the prefab, you're gonna spawn a clone of the current object every time instead of a new instance of the prefab.
I'd recommend having a EnemySpawner script in your scene, reference you prefab in that script and spawn the enemies from there.
Ok thanks Xelareip, im still new enough to Unity but ill give it a go and let you know how i get on, thanks for the reply :)
Your answer
Follow this Question
Related Questions
Error: Instantiated Enemies don't get hit 2 Answers
Spawner continuing after boss dies 1 Answer
Trigger Spawning? 1 Answer
How do I go about adding basic pathfinding to my enemy? 1 Answer
Enemy death help 1 Answer