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 /
  • Help Room /
avatar image
0
Question by Eldermos652 · Oct 12, 2015 at 08:49 PM · javascripttexture2dhealth

How to add hearts as I pick up healths?

I have a script that controls my players health. This means that if I fall off the map I lose a life and a Texture2D of a heart is reduced, you start with 3 hearts. I'm trying to whenever the player picks up a heart, its life increases, that part is working, but I can't seem to figure how to also increase the Texture2D. I've tried many ways, but nothing worked. I even tried reversing the damage part to see if it worked, but no, so I left it as it was.

Health Script:

 #pragma strict
 
 var lives = 3; //amount of health of the player
 var livesTexture : Texture2D[]; //the texture for the health
 var livesTextureSize = 30; //size for the health texture
 
 function Update () {
     if (lives == 0){
         Application.LoadLevel("Scene_1");
     }
 }
 
 function OnTriggerEnter (other : Collider) {
     if (other.tag == "Enemy"){
         if (lives-1 >= 0){
             livesTexture[lives-1] = null;
             lives -= 1;
         }
     }
     
     if (other.tag == "Falling"){
         if (lives-1 >= 0){
             livesTexture[lives-1] = null;
             lives -= 1;
         }
     }
     
     if (other.tag == "Heart"){
         if (lives+1 <= 3){
             livesTexture[lives+1] = null; //I know it doesn't work, but it was worth I shot, I guess
             lives += 1;
             Destroy(other.gameObject);
         }
     }
 }
 
 function OnGUI () {
     var livesTextureRect = Rect (10, 5, livesTextureSize, livesTextureSize);
     for (var i : int = 0; i < lives; i++){
         GUI.DrawTexture (livesTextureRect, livesTexture[i], ScaleMode.ScaleToFit, true, 0.0f);
         livesTextureRect.x += livesTextureSize + 10;
     }
 }

Taken Damage: alt text As you can see, the hearts (at the top left) was reduced and my life counter (at the Inspector) was alse reduced.

Restored Health: alt text Here's the problem, the life counter (at inspector) was increased when I picked up the heart, but the hearts stayed at two (top left).

2.png (320.9 kB)
3.png (308.5 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 Gatling-Hawk-youtube · Apr 28, 2016 at 05:05 PM 0
Share

You only have two hearts elements inspector. did you try adding three?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to create a freaking math clone. new learner and very low coding skills. Having trouble with setting the time limit for the process.Need Help? 1 Answer

Set Active Random GameObject from array (JavaScript) 1 Answer

Jimmy vegas fps tutorial converting to c# 1 Answer

Unity AssetPreview.GetAssetPreview not compiling 0 Answers

Health Script 2 Answers


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