Transform myTransform;
Transform LOL;
public float minSpeed = 5.0f;
public int score =0;
public float maxSpeed = 10.0f;
int x,y,z;
public GameObject EnemyFab;
public float currentSpeed;
bool dead;
// Use this for initialization
void Start () {
y=14;
z=-1;
if (EnemyFab == null)
{
EnemyFab=GameObject.Find (“EnemyFab”);
}
dead=false;
x=Random.Range (-14,14);
myTransform=transform;
myTransform.position = new Vector3(x,y,z);
LOL = myTransform;
LOL.position=new Vector3(Random.Range (-14,14),14,-1);
currentSpeed = Random.Range (minSpeed,maxSpeed);
}
// Update is called once per frame
void Update () {
x=Random.Range (-10,10);
myTransform.Translate (-Vector3.up*currentSpeed*Time.deltaTime);
if (myTransform.position.y < - 10) {
myTransform.position=new Vector3(x,y,z);
currentSpeed = Random.Range (minSpeed,maxSpeed);
}
}
void Respawn()
{
Instantiate (EnemyFab,LOL.position,Quaternion.identity);
}
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.CompareTag ("Lazer")){
//if the lase hits the enemy....enemy will be destroyed
Destroy (this.gameObject);
dead=true;
if(dead == true)
{
Respawn();
}
}
if(collider.gameObject.CompareTag ("Player"))
{
Destroy (this.gameObject);
}
}
}
Here is the problem :
if(dead == true)
{
Respawn();
}
You want to respawn it if dead == true, but as I can see,dead never goes false after he respawns.
Try this:
void Respawn()
{
dead = false;
Instantiate (EnemyFab,LOL.position,Quaternion.identity);
}
The problem is that you are destroying your game object (line 51) before you Instantiate your new game object. Move your Destory() call to line 60.
But assuming that this enemy is created from the same prefab, why destroy it at all? Why not just reposition it and reset any state:
void Respawn()
{
transform.position = LOL.position;
transform.rotation = Quaternion.identity;
}
There may be other state that needs to be reset as well.
I wouldn’t destroy the object. I would just reinitialize it to starting values. Create a sub to reset anything to starting values and call it when the object is dead.
public float minSpeed = 5.0f;
public float maxSpeed = 10.0f;
public int pointValue =0;
public GameObject EnemyFab;
public float maximumSpeed;
private Vector3 startingPosition;
private float currentSpeed;
private bool respawnNeeded = true;
// This assumes you assign the position according to a central spawn point before this is called.
public void Start () {
if (EnemyFab == null)
{
EnemyFab=GameObject.Find ("EnemyFab");
}
}
// Update is called once per frame
void Update () {
if(this.respawnNeeded){
Respawn();
this.respawnNeeded = false;
}
x=Random.Range (-10,10);
this.transform.Translate (-Vector3.up*currentSpeed*Time.deltaTime);
// I assume this is bounds checking?
if (this.transform.position.y < - 10) {
this.transform.position=new Vector3(x,y,z);
currentSpeed = Random.Range (minSpeed,maxSpeed);
}
}
private Vector3 CreateRandomStartPosition(){
return new Vector3(
Random.Range(-14, 14),
14,
-1
)
}
void Respawn()
{
this.transform.position = new Vector3(CreateRandomStartPosition().x, this.transform.y, this.transform.z);
this.startingPosition = this.transform.position;
//What is LOL?
//LOL.transform.position= CreateRandomStartPosition();
currentSpeed = Random.Range (minSpeed,maxSpeed);
}
void OnTriggerEnter(Collider collider)
{
if (collider.gameObject.CompareTag("Lazer"))
{
this.respawnNeeded = true;
}
if(collider.gameObject.CompareTag ("Player"))
{
this.respawnNeeded = true;
}
}