- Home /
Hi guys....i want to respawn the enemy every time it dies but it only respawns once after its death...code is given below
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);
}
}
}
Answer by robertbu · Oct 29, 2013 at 03:44 PM
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.
Thank u for replying but the following error takes place
myTransform.position = LOL.position;
myTransform.Quaternion = Quaternion.identity;
it says
Assets/Enemy.cs(70,17): error CS1061: Type UnityEngine.Transform' does not contain a definition for
Quaternion' and no extension method Quaternion' of type
UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)
Thank u anyway :)
and by changing the destroy line as u said....the enemy gets destroyed alright but the clone doesnt.....previously the clone got destroyed but after this command it doesnt and on console an error shows
UnassignedReferenceException: The variable EnemyFab of 'Enemy' has not been assigned. You probably need to assign the EnemyFab variable of the Enemy script in the inspector. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:72) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:82) Enemy.Respawn () (at Assets/Enemy.cs:69) Enemy.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/Enemy.cs:86)
thank u for ur assistance :).....plz help me out ..... i really want this to work
The first one is my fault. Code above fixed. It should have been 'transform.rotation'. As for your second error, I can take a look if you post the code. It looks like your GameObject.Find() is failing. Since this script worked before, I'm not sure what is going on. Post your script exactly as you have it along with the error, and I'll take a look. Note that with my solution, your Respawn() no longer needs an instantiate.
Here is the code for my enemy script
using UnityEngine; using System.Collections;
public class Enemy : $$anonymous$$onoBehaviour {
Transform myTransform;
Transform LOL;
public float $$anonymous$$Speed = 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;
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 ($$anonymous$$Speed,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 ($$anonymous$$Speed,maxSpeed);
}
}
void Respawn()
{
dead=false;
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);
}
}
}
And the script u gave to write
transform.position = LOL.position; transform.rotation = Quaternion.identity;
does not even create a clone....previously it used to create one.....thank u for ur help....i have sent u my enemy script u can check it :)
Answer by Crystalline · Oct 29, 2013 at 03:38 PM
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);
}
Thank u for replying but as i did this....unexpectedly it didnt respawn....problem still persists....but anyway thank u :)
and oh yes another thing as soon as the clone dies ....no clones are produced plus a msg is displayed on console
UnassignedReferenceException: The variable EnemyFab of 'Enemy' has not been assigned. You probably need to assign the EnemyFab variable of the Enemy script in the inspector. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:72) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:82) Enemy.Respawn () (at Assets/Enemy.cs:69) Enemy.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/Enemy.cs:86)
This might u give u a hint to the solution perhaps
Answer by emiller100 · Oct 29, 2013 at 07:36 PM
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.
Thank u.....but since im new to unity i might want to know the code is gonna be applied as well.....plz tell me about the code i should replace/add....:)
Answer by Dracorat · Oct 30, 2013 at 04:35 PM
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;
}
}
Hi....LOL = Transform.....what are u suggesting?....seems a bit unclear
LOL doesn't seem to belong in this class. What I gave you will work without that line. It should all work as written.
I applied ur code and it did not respawn again.....it generates an error saying
when i shoot the original enemy it says
NullReferenceException UnityEngine.Transform.get_position () (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:26) Enemy.Respawn () (at Assets/Enemy.cs:69) Enemy.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/Enemy.cs:86)
and when i shoot the clone it says
UnassignedReferenceException: The variable EnemyFab of 'Enemy' has not been assigned. You probably need to assign the EnemyFab variable of the Enemy script in the inspector. UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:72) UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:82) Enemy.Respawn () (at Assets/Enemy.cs:69) Enemy.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/Enemy.cs:86)
and then as usual does not create a new clone
I assume you dragged your enemy prefab to your scene somewhere and attached this script to it right?
Also, when you attach the script, it expects you to drag your enemy mesh to the "Enemy Fab" part of the script inspector.
Also, comment out the LOL line for now.
i did not enable my script to prefab...silly me :D
but now the scenario is that when i shoot the real one....the clone freezes in the same position i shot the enemy and then after shooting it it disappears and does not respawn.....u r getting pretty close :D