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 NEXNET_Systems · Jun 06, 2015 at 08:24 PM · guierrorplayergame

C# Script issue with Player

Hello, am currently experiencing an issue with a life player system that I'm working on. It seems whenever the player dies, the number of lives goes from 3 to -87.

It should only take away one life when the player is killed by the enemy. I have tried putting the code in onGUI void instead of Update, however upon doing so it doesn't keep track of kills for the player and the lives stay the same. And when the code is placed in the Update void it goes from 3 to -87 for one kill.

The goal is when the player dies for his lives to subtract by -1 each time until ran out then it reloads the level.

Any help with this is greatly appreciated.

Code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Lives : MonoBehaviour {
 
     public vp_FPPlayerDamageHandler player; //drag the actor into this variable in inspector
 
 
 
     //Lives System
     //private int lost = 1; //How much life should be lost when the player dies?
     public static int curLives;
     public int startinglives = 3; // How many Lives should the player start with?
     public int lost;
 
     //gui
     public GUIStyle alive;
 
 
 
     void Start()
     {
         curLives = startinglives; //Set the starting lives to default
 
     }
     
 
     void Update()
     {
 
 
         //If player dies, what's going on?
         if (player.CurrentHealth <= 0) {
             
             //If player dies, take away a life.
             //curLives-=1;
             
             Lives.curLives--;
         }
         else if (curLives == 0) {
             //If all lifes are gone, reset the level
             Application.LoadLevel(Application.loadedLevel); 
             
         }
 
     }
     
 
     void OnGUI()
     {
 
         GUI.Box (new Rect ((Screen.width)/20 -(Screen.width)/50,(Screen.height)/2-(Screen.height)/8,(Screen.width)/4,(Screen.height)/4), "Lives: " + curLives.ToString (),alive);
 
     }
 
 
 
 }
 

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
Best Answer

Answer by tanoshimi · Jun 06, 2015 at 08:27 PM

Well... in the first frame in which your player has zero or less CurrentHealth, their curLives gets reduced by one. Then, in the next frame, they have one less life, but their CurrentHealth remains the same, so they lose another life. The next frame? Lose a life. etc. etc.

I imagine that what you intended to do was that every time the player starts a new life, you restore their health. Something like:

 if (player.CurrentHealth <= 0) {
   Lives.curLives--;
   player.CurrentHealth = 100; // or whatever your max health is
 }
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

2 People are following this question.

avatar image avatar image

Related Questions

HighScore analytics 0 Answers

My game wont start after i click a button 1 Answer

In Game player message - GUIText or something else? 1 Answer

Build completed with a result of failed (can't build my android game) 2 Answers

Error when building for Android 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