• 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 Wizardman290 · Nov 18, 2014 at 01:28 AM · c#playerrespawn

How to undo destroying a component, after 5 seconds.

Hi, I am trying to make a script so if the player's health = 0, then he dies.

So far I have this:

 void KillPlayer(){
     if(Health <= 0)
         {
             Health = 0;
             Destroy(GetComponent<MeshRenderer>(), 2f);
             Destroy(GetComponent<BoxCollider>(), 2f);
             Destroy(GetComponent<PlayerMovement>());
         }
     }


And all this does, is makes it so nothing can collide with it, and it isn't visible, and can't move. How can I make the player "Respawn". I basically want to recreate the components I destroyed, and then set his position to 0, but I think that would be with a Vector 3 thing, ik how to do that part. I have no clue how to recreate components. (I want to delay the reacreation of the components by 5 seconds) If you need more info, let me know. Thanks in advance!

Comment
Add comment · Show 8
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 MrSoad · Nov 18, 2014 at 01:18 AM 1
Share

If you only want to get rid of a component temporarily then disable it rather than destroy it. You can disable the renderer, collider, and player movement script. Wait for 5 seconds, the re enable them.

 renderer.enabled = false;

etc...

avatar image MrSoad · Nov 18, 2014 at 01:32 AM 0
Share
 yield WaitForSeconds(5);

You can't use yield statements everywhere(Not in Update() for example), look them up in the docs for more info

avatar image Wizardman290 · Nov 18, 2014 at 01:32 AM 0
Share

How do I make it wait five second re enabling them, and how do I make it wait 2 seconds before it disables the renderer and collider?

avatar image Wizardman290 · Nov 18, 2014 at 01:35 AM 0
Share

Okay, so would I do

 yield WaitForSeconds(2);
 renderer.enabled = false;
 yield WaitforSeconds(2);
 collider.enabled = false
 ScriptNameWithReference.enabled = false
 
 yield WaitForSeconds(5);
 
 //Set position code
 renderer.enabled = true
 collider.enabled = true
 ScriptNameWithReference.enabled = true


?

avatar image MrSoad · Nov 18, 2014 at 01:38 AM 0
Share

Yes something very like that in a co-routine, which will accept yield statements. :)

avatar image Wizardman290 · Nov 18, 2014 at 01:38 AM 0
Share

Wait, but if I want to be constantly checking if the player has died, doesn't it need to be in update? I have this in a function that I use in Update

avatar image MrSoad · Nov 18, 2014 at 01:41 AM 0
Share

Once you have detected that the player is dead you don't need to check for that anymore, set an Is_Alive bool to false, run your death coroutine, then set your Is_Alive bool back to true at the end of the coroutine.

avatar image Wizardman290 · Nov 18, 2014 at 01:51 AM 0
Share

So have this in update:

 if(IsAlive = false)
     {
         $$anonymous$$illPlayer();
     }

and then this is my $$anonymous$$illPlayer Function + the IsAlive bool.

 bool IsAlive = false;
 void $$anonymous$$illPlayer(){
     P$$anonymous$$ = gameObject.GetComponent<Player$$anonymous$$ovement>();
     if(Health <= 0)
     {
         IsAlive = false;
         Health = 0;
         yield return new WaitForSeconds(2f);
         renderer.enabled = false;
         yield return new WaitForSeconds(2f);
         collider.enabled = false;
         P$$anonymous$$.enabled = false;
                     
         yield return new WaitForSeconds(5f);
 
         //Set position code;
         renderer.enabled = true;
         collider.enabled = true;
         P$$anonymous$$.enabled = true;
         IsAlive = true;
     }
 }

I'm obviously still new to Program$$anonymous$$g and unity, so this may not be right. Would this constantly check if the player is alive, and then when his health is = 0, then the player dies, and then after that it is checking to see if he is dead again?

Edit: After having this code put in, I get this error message:

Assets/Scripts/AttributeDeclaration.cs(157,14): error CS1624: The body of AttributeDeclaration.$$anonymous$$illPlayer()' cannot be an iterator block because void' is not an iterator interface type

(Idk what that means sorry :( )

Would you be comfortable with giving me your skype or something? so we don't have to keep communicating via my question on unity xD

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Finding the player that walked into a ray? 1 Answer

Follow Camera Interferes with Player Movement? 0 Answers

With my respawn script, Player can't move for about 5 seconds. 0 Answers

Third person movement similar to Max Payne 1 Answer


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