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
1
Question by MicDaRoc · Jun 11, 2015 at 09:25 AM · instantiatecolliderrespawnhealth

Instantiate creates endless clones and disables collider

Hi,

I'm working on a multiplayer topdown shooter in Unity2D. I duplicated the first Player to have a second controllable Character. Moving, shooting and "killing" each other works fine. Now I wanted to implement a Respawn. Whenever a character is killed, it should respawn after a certain time at the position the character was killed. I tried to do so with the following script (in the HealthScript, attached to every Player):

 public void Damage(int damageCount)
     {
         hp -= damageCount;
         
         if (hp <= 0)
         {
             Destroy(gameObject);
             Instantiate(gameObject);
         }
     }

The problem is that Unity creates a clone of the player (and names it "Player (clone)") and disables the BoxCollider2D. The bullets fly right through this character and only do damage if I enable the BoxCollider2D manually in the inspector of this clone. How do I solve this problem?

(Another Problem is that the health of this clone is at 0, instead of maxhealth of 10.)

Thanks in advance!

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
3
Best Answer

Answer by The_Toller · Jun 11, 2015 at 10:46 AM

Hello!

You're trying to instantiate an object you've just destroyed. I can't explain the process of destroying a GameObject but I see why you have trouble with it. When destroying, you're effectively removing the object from your game and since the script is attached to it, I'm surprised you get the Instantiate call running at all. I'm not going to try figuring that out, I would undoubtedly give a load of wrong and bad information.

However, I can give you another solution which would solve your problem. First of all, Instantiating and Destroying objects have their use, but since your goal is to kill and revive essentially the same object, you should really consider reusing the object you already have. Instead of destroying your object, you should just disable it. And then after a period of time, re-enable it and reset its health to the maximum value. Here's an example:

 public class PlayerRespawn : MonoBehaviour 
 {
     public int hp;                             //Current hit points
     public int maxHP = 10;                     //Maximum hit points
     public float respawnTime = 10f;            //Time to respawn
 
     public void Damage(int damageCount)        //Get a damage value
     {
         hp -= damageCount;                     //Subtract damage value from hit points
         if(hp <= 0)                            //Check to see if hit points are 0 or bellow
         {
             gameObject.SetActive(false);       //Disable this gameObject
             Invoke("Respawn", respawnTime);    //Call the method called "Respawn", 
         }
     }
 
     void Respawn()                             //Respawn method
     {
         gameObject.SetActive(true);            //Activate the gameObject
     }
 
     void OnEnable()                            //Method that runs when the gameObject is activated
     {
         hp = maxHP;                            //Set the hit points to maximum value
     }
 }

Chiao!

Note: The OnEnable method is also called when the object is created the first time. That's why I didn't, and don't need - to set the hp to an initial value. It will automatically start with maximum hit points!

Comment
Add comment · Show 3 · 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 MicDaRoc · Jun 11, 2015 at 02:09 PM 1
Share

Thanks a lot! Works like a charm!

avatar image The_Toller · Jun 11, 2015 at 05:05 PM 0
Share

Anytime! :D

avatar image The_Toller · Jul 20, 2015 at 05:55 PM 0
Share

Please mark my answer as the answer if it helped you. :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Respawn Player Using a Coroutine 0 Answers

Respawn Management 2 Answers

Player Respawn After Death 1 Answer

loss of life help 1 Answer

My object don't spawn as expected so what should I do? 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