Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 convictcartel · Jan 15, 2014 at 10:02 AM · enemyrespawnenemy spawnrespawner

Respawn Enemy Prefab after its destroyed

Ive added a script onto my enimes that makes it once they are destroyed they will respawn the rigidbody placed in "respawn" and at the spot "respawnpoint" my problem is that once respawned they are called "Clones" and after two respawns the enemy no longer has the scripts working.. how do I edit this script to spawn my enemy not as a clone?

 using UnityEngine;
 using System.Collections;
 
 public class AttackandHP : MonoBehaviour {
     public int maxHealth = 10;
     public int curHealth = 10;
     
     public GameObject target;
 
 
     public float attackTimer;
     public float coolDown;
     public float EXPGAIN = 20;
     public GUIText DamagePoints;
     public int damage = 0;
     public Transform ptsPrefab; 
 
     public Rigidbody respawn;
     public Transform respawnpoint;
 
         public float healthBarLength;
     
         
     // Use this for initialization
     void Start () {
 
         target = GameObject.FindGameObjectWithTag ("Player");
 
     healthBarLength = Screen.width / 2;
     
     attackTimer = 0;
     coolDown = 2.0f;
 
 
     }
     
     // Update is called once per frame
     void Update () {
 
     AdjustCurrentHealth(0);
     
     if(attackTimer > 0)
             attackTimer -= Time.deltaTime;
             
         if(attackTimer < 0)
             attackTimer = 0;
             
         if(attackTimer == 0) {
             Attack();
             attackTimer = coolDown;
             }
         
     }
     
     void OnGUI() {
 
     }
     
     public void AdjustCurrentHealth(int odj) {
     curHealth += odj;
     
     if(curHealth < 1)
         curHealth = 0;
         
     if(curHealth > maxHealth)
         curHealth = maxHealth;
         
     if(maxHealth < 1)
         maxHealth = 1;
     
     if (curHealth == 0) {
                         Destroy (gameObject);
 
             Rigidbody clone;
             clone = Instantiate (respawn, respawnpoint.transform.position, respawnpoint.transform.rotation) as Rigidbody;
 
                         PlayerAttack ZS = (PlayerAttack)target.GetComponent ("PlayerAttack");
                         ZS.DeselectTarget ();
                         ZS.SpawnEXP();
                         PlayerHealth eh = (PlayerHealth)target.GetComponent ("PlayerHealth");
                         eh.AdjustCurrentEXP (20);
 
 
                 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by nesis · Jan 15, 2014 at 01:52 PM

You're destroying the enemy gameObject and are replacing it with something that doesn't necessarily have all components your original gameObject had, because it's being created from a Rigidbody.

Save the enemy as a Prefab, change respawn's type to be GameObject, and then after selecting the enemy in the Inspector, set respawn's value to whatever the enemy's prefab is in the Hierarchy tab. That way you'll be spawning fully-fledged copies of your enemy.

That said, you seem like you don't want to destroy the enemy so much as reset its health and other stats, and place it back at its spawn point. So instead of using Destroy(gameObject), you could just write a method to reset the enemy's stats, position, and orientation to what they were when the enemy first spawned.

For a quick and dirty example:

 using UnityEngine;
 using System.Collections;

 public class Enemy : MonoBehaviour {
     
     private Vector3 startPosition;
     private Quaternion startRotation;
     private float startHealth;
     public float health = 50f;
     
     public void Start() {
         //record starting values of relevant variables
         startHealth = health;
         startPosition = transform.position;
         startRotation = transform.rotation;
     }
     
     public void TryDamage(float amount) {
         health -= amount;
         if (health<0f) {
             KillAndReset();
         }
     }
     
     //call this when the enemy needs to be killed and reset
     public void KillAndReset() {
         health = startHealth;
         transform.position = startPosition;
         transform.rotation = startRotation;
     }
 }
Comment
Add comment · Show 1 · Share
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 convictcartel · Jan 17, 2014 at 05:12 AM 0
Share

Thanks man, worked perfect~!

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

19 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

Related Questions

How kill an enemy and respawn after die 2 Answers

Enemy not generating... 0 Answers

Enemy respawning after death 1 Answer

enemy respawn after amount of time 2 Answers

I Can't make 3D Enemy Random Spawn and this Random Moving.. 0 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