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 kiko · Sep 11, 2011 at 04:24 PM · health-deductionhealth

irregular decrease of the lives in health control

hi guys i have a health control script thats have some problem,when i play the game its working fine by minus 1 lives but when i replay the game its minus 3 lives or even 6 lives. i do not know why i suspect is because i have 2 different damage style that make the health control script crash, one is fallout the other one is bullet.

here my health control script:

var health3 : Texture2D; var health2 : Texture2D; var health1 : Texture2D;

var player : Transform;

static var Lives = 3; private var playerInfo : ThirdPersonStatus;

// Cache link to player's state management script for later use.

function Awake() { playerInfo = FindObjectOfType(ThirdPersonStatus); if (!playerInfo) Debug.Log("No link to player's state manager."); }

function Update() { print("Lives: "+Lives+" Hits: "+HITS); switch(Lives) { case 3: guiTexture.texture = health3; break; case 2: guiTexture.texture = health2; break; case 1: guiTexture.texture = health1; break; case 0: Application.LoadLevel("3"); break; } and here my thirpersonststus:

function LateUpdate() { if(dead) { transform.position = Vector3(639,39,427); gameObject.Find("Main Camera").transform.position = Vector3(639,39,-427); dead = false; } }

function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.gameObject.tag == "fallout") { dead = true; HealthControl.Lives -= 1; }

     else if(hit.gameObject.tag == "bomgy 1")
     {
     dead = true;
     HealthControl.Lives -= 1;
     
     }
     
     if(hit.gameObject.tag == "spike")
     {
     dead = true;
     HealthControl.Lives -=1;
     }
 }

Comment
Add comment · Show 4
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 Rennat · Sep 11, 2011 at 06:15 PM 0
Share

please fix your code formatting, there is a code block button ( icon is 0s and 1s )

avatar image Ludiares.du · Sep 11, 2011 at 06:28 PM 0
Share

You're not setting anywhere your live to be equal 3

avatar image kiko · Sep 12, 2011 at 04:40 AM 0
Share

var Lives = 3;

var Score : int = 0;

static var playerScore : int;

//static var HITS = false;

static var dead = false;

// sound effects. var struckSound: AudioClip; var deathSound: AudioClip;

var Script : HealthControl;

private var heart = false;

var bullitPrefab : Transform;

function LateUpdate() { if(dead) { transform.position = Vector3(639,39,427); gameObject.Find("$$anonymous$$ain Camera").transform.position = Vector3(639,39,-427); dead = false; } }

function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.gameObject.tag == "fallout") { dead = true; HealthControl.Lives -= 1; }

     else if(hit.gameObject.tag == "bomgy 1")
     {
     dead = true;
     HealthControl.Lives -= 1;
     
     }
     
     
     if(hit.gameObject.tag == "spike")
     {
     dead = true;
     HealthControl.Lives -=1;
     }
avatar image kiko · Sep 12, 2011 at 04:41 AM 0
Share

what you mean by not setting anywhere?how to make Lives to be equal 3?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kacer · Sep 12, 2011 at 10:58 AM

If you're replaying the game by using the "application.loadlevel" then the lives wont get reset to 3 or whatever your default value is.

Because.

you're using a static variable as the lives, Static variables dont get reset between level loads because thats the way they work (there's some longer explanation as to why, but i really cant remember it >_<), what you can do in the "start function" of this script, maybe "awake" even, is to set lives to their default value, that should fix it.

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 kiko · Sep 13, 2011 at 03:25 AM 0
Share

thanks but i don't understand what you mean? how to i write the function awake to? is it from the the health control or from the thirdpersonstatus or the loadapplication script.Can show me an example?thank you

avatar image Kacer · Sep 13, 2011 at 07:53 AM 0
Share

try writing this in the beginning of the script where your health variable is declared:

function Start(){

Lives = 3;

}

avatar image kiko · Sep 13, 2011 at 02:08 PM 0
Share

thank you for the example i solve already one last question how do i reset back the Lives back to 3 when i replay the game?

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

Implementing Damage to Gun, best way to implement health and damage. 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

enemy attack not recognizing players stats 1 Answer

insert semicolon 2 Answers

How to deplete health every second 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