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
1
Question by Avlas99 · Apr 13, 2018 at 01:41 PM · platformerrespawn

Enemies respawn in platform game

So I made this really simple android platform game.

Inside each level, there's a checkpoint and, if the player dies after reaching the checkpoint, I want all of the enemies to be brought back to their starting position with full health.

Each enemy has a script, inside this script there's a function that is called every time the enemy or the player dies. This function should bring the enemy back to its starting position with full health.

And here's the problem: for some reason, the script works for some of the enemies and doesn't work for others.

 void Reset(){
         if (curHealth <= 0) {//if the enemy dies
             gameObject.SetActive (false);//deactivate the enemy game object
         }
         curHealth = maxHealth; //set the enemy's health to maximum
         gameObject.transform.position = startPosition; //set the position of the enemy to its starting position
     }
 void Start()
     {
         startPosition = gameObject.transform.position; //set the variable "startPosition" to the starting position of the enemy
 }
 void FixedUpdate()
     {
         if (curHealth <= 0 || Player.curHealth <= 0){//if the player OR the enemy die
             Reset();
         }
 }

Any ideas on what could I be doing wrong?

EDIT: What happens is that some of the enemies just remain in their position and their health remains the same after the death of the player

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 Scribe · Apr 13, 2018 at 06:39 PM 0
Share

How are you finding the objects to call reset on? If you set them to inactive the usual FindByX methods won't pick them up.

1 Reply

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

Answer by shadowpuppet · Apr 13, 2018 at 06:37 PM

I don't think you need FixedUpdate for that. Update or even LateUpdate. There isn't anything setting the enemy health to ZERO if the player dies ,so there is nothing to reset for the surviving enemies.

 if(Player.curHealth <=0){
 curHealth = 0;// the enemy health, I presume
 Reset();
 }
Comment
Add comment · Show 4 · 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 Avlas99 · Apr 13, 2018 at 07:28 PM 1
Share

Thanks. I spent so much time on trying to solve this, can't believe it was that easy, thank you again!

avatar image shadowpuppet Avlas99 · Apr 15, 2018 at 09:10 PM 0
Share

glad I could help. If you accept the answer you should mark it so . Just sayin because i was bored today and thought I would try to answer some unanswered questions and came across this again. It also helps people with similar problems see that this post may supply an answer. But again, glad I could help. Pretty new at program$$anonymous$$g so usually my answers are shot down - which is why I try to find those questions no one else has bothered to respond to.

avatar image Avlas99 shadowpuppet · Apr 15, 2018 at 09:12 PM 0
Share

Sure, I'm sorry but I don't really know how Unity Answers works. I also gave you some kind of "point". Have no idea what it is but it looked like a nice thing to do, lol

Show more comments

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

81 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

Related Questions

2D Platformer - How do I have objects that kill the player upon touch/cause respawn? 2 Answers

Save game progress at respawn points 2 Answers

[2D] [C#] Player dying at respawn at checkpoint 1 Answer

Error says object reference not set when it really is? 1 Answer

I'm trying to make a "Game Over" sound when the player dies and respawns 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