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
0
Question by unity_LAfgjMs60GPE7w · Apr 28, 2018 at 01:38 PM · script.keyboardhealth

health script do not work when collision detected

alt text I wanted to create a health system for my first game it works like this when collision detected and keyboard button pressed the heart is deactivated I don't know what's the problem here's my code . #pragma strict var currentHealth : int; var maximumHealth : int = 3 ;

 var heart1:GameObject;
 var heart2:GameObject;
 var heart3:GameObject;
 
 function Start()
 {
     currentHealth = maximumHealth ;
 }
 
 function Update()
 {
 
     if(currentHealth<0)
     currentHealth = 0;
     if(currentHealth>maximumHealth)
     currentHealth = maximumHealth;
     if(currentHealth == 3)
     {
         heart1.SetActive(true);
         heart2.SetActive(true);
         heart3.SetActive(true);
     }
 
     if(currentHealth == 2)
     {
         heart1.SetActive(true);
         heart2.SetActive(true);
         heart3.SetActive(false);
     }
 
     if(currentHealth == 1)
     {
         heart1.SetActive(true);
         heart2.SetActive(false);
         heart3.SetActive(false);
     }
 
     if(currentHealth == 0)
     {
         heart1.SetActive(false);
         heart2.SetActive(false);
         heart3.SetActive(false);
         GameOver();
     }
 
 
 
 }
 
 
 function GameOver()
 {
 Debug.Log ("collsion detected");
     Application.LoadLevel("play 2);
 }
 
 
 
 
 
 
 
 function OnTriggerStay2D(autre : Collider2D ){
         Debug.Log ("collsion detected");
         if (autre.gameObject.tag=="nine") {
             Debug.Log("planet detected");
             if (Input.GetKeyDown(KeyCode.Space)) {
             currentHealth = currentHealth - 1;
             }}}
 


capture.jpg (45.4 kB)
capture.jpg (116.4 kB)
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 Willard720 · Apr 28, 2018 at 02:58 PM 0
Share

This probably won't fix your problem, but you're code was in dire need of being refactored, so here:

 #pragma strict 
 var currentHealth : int; 
 var maximumHealth : int = 3 ;
 var hearts:GameObject[];

 function Start() { currentHealth = maximumHealth; }

 public function Damage(int amount) { SetHealth(currentHealth - amount); }
 public function Heal  (int amount)     { SetHealth(currentHealth + amount); }

 private function SetHealth(int newHealth)
 {
     currentHealth   = newHealth < 0 ? 0 : newHealth > maximumHealth ? maximumHealth : newHealth;
     int tempHealth = currentHealth;

     for (int i = 0; i < hearts.Length; i++)
         hearts[i].SetActive(tempHealth-- >= 0);

     if (currentHealth <= 0) GameOver();
 }

 function GameOver()
 {
     Debug.Log("collsion detected");
     Application.LoadLevel("play 2");
  }

 function OnTriggerStay2D(autre : Collider2D )
 {
     Debug.Log("collsion detected");

     if (autre.gameObject.tag != "nine") return;
     Debug.Log("planet detected");

     if (!Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space)) return; 
     Damage(1);
 }

I'm not all that familiar with JavaScript and this is untested, so it's more like pseudo code, but you get the idea. There were a few big problems with your code. One of them was how you were checking your health, every single frame. You're not the first Unity developer I've seen that thinks you can just through things on the Update function and get away with it. If you keep doing that for every single class, you're performance will start to drop. It's bad practice and organization. You need to use more abstraction in your code.

ins$$anonymous$$d, have a Damage and Heal function. These are simple and public, and both call on the private SetHealth who does the heavy lifting. I used a neccessary ternary operator to clamp the value of currentHealth before we assign it. Ternary operators are awesome and you should look up more about them.

You then had 3 variables for heart. Sure, that may have worked, but what if you wanted to make 5, or 10, or 500 hearts? This new code is much cleaner, uses less lines, and easier to expand upon. I'm using an array to contain the three hearts. I then use the for loop to loop through all of them.

The tempHealth buisness is what I use to deter$$anonymous$$e if we should show the heart or not. If tempHealth starts at 2, the program will check if 2 is greater than zero (which will be true) so the first sprite is visible. It then decreases the 2 to 1 and checks again. Again, the next sprite is visible. It decreases again, and now, tempHealth is 0, so the third heart is invisible. So now, you can easily add 50 hearts if you wanted, without having to change the code.

Also, ins$$anonymous$$d of currentHealth = currentHealth - 1 you can do currenthealth -= 1 or even `currentHealth--;

1 Reply

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

Answer by m-akrami · Apr 28, 2018 at 02:55 PM

hey , the GetKeyDown method only run once when you press a key down , if you pressing the button before entering the collision this line not going to run :

    if (Input.GetKeyDown(KeyCode.Space)) {
              currentHealth = currentHealth - 1;
              }

you can do this with one bool variable in update function like this :

    if (Input.GetKeyDown(KeyCode.Space)) {
              spacePressed = true;
              }
 
    if (Input.GetKeyUp(KeyCode.Space)) {
              spacePressed = false;
              }

and then check this bool in your collision function like this:

  function OnTriggerStay2D(autre : Collider2D ){
          Debug.Log ("collsion detected");
          if (autre.gameObject.tag=="nine") {
              Debug.Log("planet detected");
              if (spacePress) {
              currentHealth = currentHealth - 1;
              }}}



Comment
Add comment · Show 1 · 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 unity_LAfgjMs60GPE7w · Apr 28, 2018 at 03:36 PM 0
Share

thank you man you saved my life

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

89 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"Keyword already used" error message by voice recognizer script 1 Answer

Trouble setting a parameter with a collision 1 Answer

Keyboard detector 0 Answers

Only destroy one instatnce 1 Answer

Easy sprite finding script 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