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.
– gajdotyes a agree with gajdot, just make your
– ssprossenemyvariable public and then drag the prefab on it in the inspectorI've made it public and the enemy was a prefab so unfortunately it didn't fix my problem, thanks for the response though.
– JayFitz91