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 Dustyman567 · Apr 11, 2015 at 02:15 AM · c#buttonenemyhealth

Int value only changes after I stop playing

I made buttons that when I press them. it lowers the hp of the enemy in the scene, however when I press them nothing happens until I press the play button again to stop playing. After I do this it changes the int value and dosen't reset it to the default of 5. I put 5 buttons in the scene and clicked them all and nothing happened, when I pressed play again to end it and then hit play again it loaded the lose scene. Here is the code in question:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyHealth : MonoBehaviour
 {
     public int ehp = 5;
 
         
     void Update() {
         if (ehp <= 0) {
             Application.LoadLevel("Lose Screen");
 
 }
     }
 
 }
 `
 
 and this:
 
 `using UnityEngine;
     using System.Collections;
     public class ButtonInteraction : MonoBehaviour
     {
         public EnemyHealth _enemyHealth;
         public void DamageButton(int damage)
         {
                     _enemyHealth.ehp -= damage;
             }
     
     }
 

The first is the script on the enemy and the second is the script on the button. Can someone help me with this?

Comment
Add comment · Show 2
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 YoungDeveloper · Apr 11, 2015 at 02:17 AM 0
Share

I formatted the code for you.

avatar image Pharaoh_ · Apr 11, 2015 at 02:28 AM 0
Share

First off, don't use an Update to check whether you are meeting the health criteria. Check in your DamageButton() method:

 _enemyHealth.ehp -= damage;
 if (_enemyHealth.ehp <= 0) {
     Application.LoadLevel("Lose Screen");
 }

As for the problem, in the Inspector, have you added the function in the OnClick() field?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by YoungDeveloper · Apr 11, 2015 at 02:22 AM

Update() is called every frame, use it only for things which must be updated.

In your case simple check will do. I made the revision for you.

 using UnityEngine;
 using System.Collections;
  
 public class EnemyHealth : MonoBehaviour{

     public int health = 5;
 
     public void remove(int value){
         health -= value;

         if(health <= 0){
             health = 0;
             Application.LoadLevel("Lose Screen");
         }   
     } 
 }


  using UnityEngine;
  using System.Collections;
  public class ButtonInteraction : MonoBehaviour{

      public EnemyHealth _enemyHealth;

      public void DamageButton(int damage){
          _enemyHealth.remove(damage);
      }
  
  }
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 Dustyman567 · Apr 14, 2015 at 04:29 PM 0
Share

Ok now I have a new problem. The health still stays at 0 after I end like it was before. I need to have everything for the button to be on a prefab because I instantiate it into the scene, so I can't apply the actual enemy in the scene to the button health thing, I have to apply the enemy prefab to the button which I think is what is causing this problem. Is there a way I can apply the enemy in the scene and not its prefab to the prefab of the button or at the very least check on the enemy in the scene's health in the script or something like that?

avatar image YoungDeveloper · Apr 15, 2015 at 10:07 AM 0
Share

Sounds like "prefabs and enemies" is a completely separate question (which should be a new question) and has nothing to do with this.

avatar image Dustyman567 · Apr 16, 2015 at 03:25 PM 0
Share

yeah I thought so. I will post a new question about this.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Enemy atributes script 1 Answer

Enemy Health GUI Issues 0 Answers

Distribute terrain in zones 3 Answers

Need help for programming the killing of an enemy 1 Answer

(C#) Enemy health and take damage from bullets 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