- Home /
Help with error when using instantiate to respawn an enemy.
So i've been working on respawning my enemies for a while now and I'm trying to get it working. Currently i have my scene set up with an empty game object with this script assigned to it to allow an enemy to spawn and respawn after dying.
public float timer;
public float reloadLength;
private GameObject chicken;
public GameObject respawn;
private bool reload = false;
private enemyHealth health;
// Use this for initialization
void Start()
{
chicken = (GameObject) Instantiate(respawn, new Vector3(-5, 0, 0), Quaternion.identity);
chicken.transform.parent = gameObject.transform;
}
// Update is called once per frame
void Update()
{
/* if(health.enemyCurHealth <= 0f){
waitToReload = reloadLength;
StartCoroutine("Timer");
Destroy(chicken);
}*/
}
public void died(){
Debug.Log ("Died");
respawning ();
Destroy(chicken);
}
public void respawning(){
//timer += Time.deltaTime;
//if(timer > reloadLength){
chicken = (GameObject) Instantiate(respawn, new Vector3(-5, 0, 0), Quaternion.identity);
chicken.transform.parent = gameObject.transform;
//timer = 0;
//}
}
/*IEnumerator Timer()
{
Debug.Log ("Timer Started");
yield return new WaitForSecondsRealtime(waitToReload); // here, function wait the specified time on secons
chicken = Instantiate(respawn, new Vector3(-5, 0, 0), Quaternion.identity);
chicken.parent = transform;
StopCoroutine ("Timer"); // Exit of loop Timer , very important
}*/
}
the chicken gam object is empty and the respawn object is set to the chicken prefab.
This next script is attached to my enemy prefab for its health.
public int enemyMaxHealth = 100;
public int enemyCurHealth = 100;
public float waitToReload;
public float reloadLength;
private bool reload = false;
private playerStats thePlayerStats;
private NewEnemyHealth dead;
public int expToGive;
// Use this for initialization
void Start () {
enemyCurHealth = enemyMaxHealth;
thePlayerStats = FindObjectOfType<playerStats> ();
dead = GetComponent<NewEnemyHealth> ();
}
// Update is called once per frame
void Update () {
if (enemyCurHealth <= 0) {
thePlayerStats.addExp (expToGive);
dead.died ();
}
}
public void HurtEnemy( int damageToGive){
enemyCurHealth -= damageToGive;
}
public void setMaxHealth(){
enemyCurHealth = enemyMaxHealth;
}
}
As you can see when health hits 0 it calls upon the die() method in the first script. It works fine as it spawns the first one and i can do damage but once the health hits 0 it spawns a new one and then freezes with this error
InvalidCastException: Cannot cast from source type to destination type. UnityEngine.Object.Instantiate[GameObject] (UnityEngine.GameObject original, Vector3 position, Quaternion rotation) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEngineObject.cs:206)
at the start and died methods in the script. Any help from anyone would be awesome it is really frustrating me! thanks a ton!
Try using Instantiate<GameObject>()
ins$$anonymous$$d of casting.
If that doesn't work, which line exactly is causing the error? The editor should tell you.
Your answer
Follow this Question
Related Questions
enemy respawn after amount of time 2 Answers
instantiated objects animation making the object reset to 0,0,0?? 1 Answer
My object don't spawn as expected so what should I do? 0 Answers
Instantiate Explosion Position 2 Answers
Respawn Management 2 Answers