Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by austinkat · Aug 19, 2017 at 01:00 AM · instantiateenemyerror messagerespawn

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nomenokes · Aug 21, 2017 at 05:11 PM 0
Share

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.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges