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 shanereichenfeld · Jan 03, 2014 at 06:12 AM · damagehealth

Health bar losing health

 GameManager.cs
 
 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour {
 
     //Player Life
     public Texture playersHealthTexture;
 
     //Control Screen Position of Texture
     public float screenPositionX;
     public float screenPositionY;
 
     //Controls Icon Size on Screen
     public float iconSizeX = 25;
     public float iconSizeY = 25;
 
     //Starting lives
     public int playersHealth = 5;
     
     void OnGUI(){
 
         //Controls Players Health Textures
         for(int h = 0; h < playersHealth; h++){
             GUI.DrawTexture(new Rect(screenPositionX + (h * iconSizeX), screenPositionY, iconSizeX, iconSizeY), playersHealthTexture,ScaleMode.ScaleToFit, true, 0);
         }
     }
 
     void PlayerDamaged(int damage){
 
         if (playersHealth > 0) {
             playersHealth -= damage;
         }
 
         if(playersHealth <= 0){
             playersHealth = 0;
             RestartScene();
         }
     }
 
     void RestartScene(){
         Application.LoadLevel(Application.loadedLevel);
     }
 
 }
 
 
 Enemy.js
 
 #pragma strict
 
 private var fall : boolean;
 var Player : GameObject;
 var spawnPoint : Transform;
 var stomp : boolean;
 
 function Update () {
     if(stomp){
         transform.position.z = 4;
         transform.localScale.y /= 2;
         fall = true;
         gameObject.GetComponent(PlatformMover).step = 0.0;
         stomp = false;
         
     }
     
     if(fall){
         transform.position.y -= 0.05;
     }
     if(transform.position.y < -25){
         Destroy(gameObject);
     }    
 }    
     
 function OnTriggerEnter(other : Collider){
     if(!stomp){
         if(other.tag == "Player"){    
             Destroy(other.gameObject);
             var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
             var sf = Camera.main.GetComponent(SmoothFollow2);
             sf.target = P.transform;
         }    
     }        
 }
 
 
 
 ok so far i have my health bar showing But i 
 need it to subtract 1 out of 5 hearts every time
  the enemy kills my player. 
 Can someone help me out with finishing that code and show me where to put it?
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Imankit · Jan 03, 2014 at 06:23 AM

You can write a OnTrigger Function in the Player Script. Whenever the player enters the enemy's collider call the PlayerDamage Function.. Example -

 //In your Player Script

 void OnTriggerEnter(Collider col){
 if(col.collider.name == "Enemy"){
 GameManager.PlayerDamage();//Make the PlayerDamage Function static or call it by instance
 }
 }
Comment
Add comment · 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

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

19 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

Related Questions

Health below zero but shows negative numbers 1 Answer

Object Not Recieving Damage 3 Answers

Expecting ) found = 1 Answer

Enemy Health Damage 2 Answers

Object reference not set to an instance of an object 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