Destroy and Spawn an Enemy

Hello,

I am working on a

  1. Side scroller ( meant that the view is 2D) - only on X-Y plane. Z always ‘0’.
  2. The canon has to be placed on the left most corner and fire in a Projectile Fashion

Game Objects :

I have placed a script called as “enemypos” on an empty gameobject. Then there is another prefab called as the “enemy” which is the enemy.

Code:


The enemypos script is as follows :

var projscript : projectile;
var ene_pos: Transform;
var pos : float;


function Awake()
{
	e_spawn();
}


function Update ()
{
	

}


function e_spawn()
{
	pos = projscript.spawn();
	var position: Vector3 = Vector3(pos, 1, 0);
    var enemy = Instantiate(ene_pos, position, Quaternion.identity);
}

The spawn() function is as follows: (is called from another script)

 function spawn()
    {
    	if(Random.Range(0,10)<7)
    		return Random.Range(-18,10);
    	else
    		return Random.Range(10,21);
    	
    }

Code Explanation:

The function e_spawn() is used to instantiate the enemy prefab.

Request:

My humble request is how to destroy and instantiate the same enemy along the x axis.

THANK YOU in advance

If I understand well, in the function spawn() you randomize the point on which the new enemy will appears, and you wanna modify that script. If so:

 var enemy : GameObject;
 var y = 0 //this is a constant value, I think...

 function spawn()
    {
        if(Random.Range(0,10)<7)
           newX = Random.Range(-18,10);
        else
           newX = Random.Range(10,21);
        
        Instantiate(enemy, Vector3(newX,y,0), Quaternion.identity);
    }

I think that it could be a solution for your problem.