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 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. :)

Show more comments

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

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

Third person movement similar to Max Payne 1 Answer

Loading a new level from a previous one. 1 Answer

Respawn Script Problem 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