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 NiallBeast · May 14, 2014 at 06:08 PM · enemieshealth

How to stop enemies dying on spawn

I am relatively new to unity and C#. I am trying to make a simple dungeon crawling game. Everything is working fine, apart from the code for my enemies health. the if statement telling the game to destroy the enemy game object makes them die as soon as the game is played. An identical piece of code is used for the player and this works fine. any help would be greatly appreciated. code is below, the part that isn't working is the comment at the end. Thanks in advance!

 public class StrEnemyHealth : MonoBehaviour {
 
     
     int StrEnHealth = 20;
     
     void OnTriggerEnter(Collider collision)
     {
         //takes health from enemy
         if (collision.gameObject.tag == "Player")
         {
             StrEnHealth-=PlayerStats.Att;
             Debug.Log("enemy health:" +StrEnHealth);
         }
     }
 
     /*
     //kills enemy if health is 0
     void Update()
     {
         if (StrEnHealth <= 0);
         {
             Debug.Log("enemy dead!");
             Destroy(gameObject);
         }
     }
     */
 
 }
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 NiallBeast · May 14, 2014 at 06:50 PM 0
Share

I thought nothing else could access this variable because it is not public? i have looked through the code for everything else, and nothing is accessed again.. I have also double checked all tags and everything is correct.. I really am at a loss!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ThePunisher · May 14, 2014 at 06:11 PM

Edit:

Found the problem. The if statement has a semicolon appended to the end of it, and it shouldn't. Change it to the following code:

 if (StrEnHealth <= 0)
        {
          Debug.Log("enemy dead!");
          Destroy(gameObject);
        }
 

What's happening when the semicolon is in there is the if statement evaluates but does absolutely nothing. Then the code in the curly braces will always execute regardless of the evaluation result of the if statement.

Comment
Add comment · Show 7 · 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 NiallBeast · May 14, 2014 at 06:15 PM 0
Share

Hi, yeah i'd noticed this seconds after i posted! thanks for the help but still hasnt fixed the problem

avatar image ThePunisher · May 14, 2014 at 06:19 PM 0
Share

What is occurring now? Are there any other scripts on the enemy game objects? Or perhaps the enemies are tagged as "Player" and are colliding with each other?

avatar image NiallBeast · May 14, 2014 at 06:27 PM 0
Share

the same is happening.. on play, the enemies die before the game even starts, the debug console just lists all enemies have died. there is an AI script that controls movement, an asset i have found online. if i leave that end portion of code commented out, everything works perfectly, apart from the enemies not dying.

avatar image ThePunisher · May 14, 2014 at 06:36 PM 0
Share

Well, we know two things so far.

1) The enemies only die if the update code is uncommented. This means that nothing else is accidentally killing your enemies (its a good thing).

2) The only way for your enemies to die if the update code is uncommented is if the enemy's health reaches a value that is less than or equal to 0.

One thing to do is check anything that modifies that health value, especially outside of that script. If nothing outside of this script modifies the health value, then we know one more thing. The only way for the health value to be reduced is when a game object tagged as "Player" collides with the collider of the object this script is on. That means that your enemy's must be somehow colliding with your player or something tagged as "Player" upon starting. Ensure nothing else is accidentally tagged as player.

avatar image ThePunisher · May 14, 2014 at 06:40 PM 0
Share

I think I found the problem, check the updated answer.

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

21 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

Related Questions

Instantiation array of enemies, c# ? 2 Answers

I can't have more enemies at the same time. 1 Answer

Arrays of Enemies in a Game HELP! 0 Answers

Changing Mesh of an object depending on Health Value. C# 2 Answers

I made my code so that if i hit an enemy its health will go down but when i hit the enemy the health doesnt go down. please help 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