- Home /
Destroy and Spawn an Enemy
Hello,
I am working on a
Side scroller ( meant that the view is 2D) - only on X-Y plane. Z always '0'.
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
Fellow Unity Users - $$anonymous$$y original question and it's explanation was wrong but now I have edited it.
Could someone kindly help me on this?
Answer by BiG · Dec 07, 2011 at 08:02 AM
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.
Sorry mate - Really sorry I messed up things. I was so confused that I made something else on my Question. I should not have done that. I have changed the question right now. If it was not you, I do not think I could have noticed it. I hope I am clear now.