Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 rOBY GAMES · Jul 24, 2015 at 09:44 PM · playerlevellifedeadhealth

Life Player loads another level

Hello everyone.

I'm editing a script to allow the character to die three times, and then load the level gameover. I am using this script HealthBar, to allow after 3 times the player dies, the charge level should gameover. Suggested some? Thank You.

healthbar script:

 using UnityEngine;
 using System.Collections;
 
 public class HealthBar : MonoBehaviour 
 {
 
     public Player Player;
 
     public static int life = 5;
 
     public Transform ForegroundSprite;
     public SpriteRenderer ForegroundRenderer;
     public Color MaxHealthColor = new Color(255 / 255f, 63 / 255f, 63 / 255f);
     public Color MinHealthColor = new Color(64 / 255f, 137 / 255f, 255 / 255f);
 
 
     public void Start()
     {
 
     }
     
     public void Update () 
 
         {
         life += 3;
         
         var healthPercent = Player.Health / (float) Player.MaxHealth;
     
         ForegroundSprite.localScale = new Vector3(healthPercent, 1, 1);
         ForegroundRenderer.color = Color.Lerp(MaxHealthColor, MinHealthColor, healthPercent);
 
         life -= 1;
         
         if(life <= 0) 
         {
             Application.LoadLevel("Resuscita Argon");
             
             life --;
             GameObject.Find("Vita3").guiText.text = ""+life;
             print("Prova Vita" + life + " Cazzo non funziona!!!");
         }
     
     }
 }


script count life:

 using UnityEngine;
 using System.Collections;
 
 public class Contovita : MonoBehaviour {
 
     public static int life = 5;
     
     // Update is called once per frame
     void Awake () {
          
         guiText.text = ""+3;
 
         print(life);
     
     }
 }
 

Comment
Add comment · Show 5
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 Hexer · Jul 24, 2015 at 10:06 PM 0
Share

I am sorry but I don't understand it. What is your question? You want help with editing the existing script to your desire?

avatar image rOBY GAMES · Jul 24, 2015 at 10:18 PM 0
Share

Hello Hexer. Currently the two scripts are not working.They have already changed. In practice when I start the game to test the first death of player number 3 appears on the screen it is 3, then the player disappears. You think that the script are correct? I have error? thank you

avatar image Hexer · Jul 24, 2015 at 10:55 PM 0
Share

These scripts are faulty, for what you want to accomplish. Life will actual never hit zero. Because in the Update it will change the int "life" with +3 and -1. resulting in +2 each update.

avatar image rOBY GAMES · Jul 25, 2015 at 10:06 PM 0
Share

in this script that name should I give? where do I add it?

  void LifeReduction(){
  //Remember that you have to invoke "LifeReduction" within another function.
  //How you do this depends on the condition, by colliding? raycast? or a loop?
  //Reduces Life with -1.
      Health.life -= 1;
      }
avatar image Hexer · Jul 25, 2015 at 10:27 PM 0
Share

You can name the script whatever you want. Where you put this script depends on the method you want to use to reduce the lives of the player. The important part of the script is (Health.life -=1;) which reduces the life with 1 . You can put that line of code in an OnTriggerEnter(Collider other) function and attach that to the player.

For example if you have a player and an obstacle tagged as "spikes" you just do.

 void OnTriggerEnter(Collider other){
 if(other.tag == spikes){
 Health.life -= 1;
 }
 }

for each time the player walks into a collider with the tag "spikes" it will reduce the life with 1 Be sure to tag the obstacle with "spikes" and put a collider on it and have the isTrigger ticked to true. Then check if the player has also a collider and attach a rigidbody to the player.

1 Reply

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

Answer by Hexer · Jul 24, 2015 at 10:36 PM

I guess you picked a script from Internet and want to edit it some bit. If you did then you will see that you don't need some codelines.

I wrote a sample script for you.

     using UnityEngine;
      using System.Collections;
      
      public class Health : MonoBehaviour 
 
     //Total Lifes you start with.
     public static int life = 3;
     
     void Update(){
 //If you have zero lives left it will go to the Level " GameOver"
 //The Scene called "GameOver" should be added to the build and run scene.
     if(life <= 0){
     print("GameOver, You have no lives left")
     Application.LoadLevel("GameOver");
     }
     }

in another script. // If a condition is met, invoke " LifeReduction" (This will reduce the lives you have left with -1)

  //If you want to invoke this function within another script, then it is needed to add static public before void.
  //Invoke this functiom can be done by doing someting like X.LifeReduction();
  //X is the variable for how you named the script where you put this code.
 static public void LifeReduction(){
 //Remember that you have to invoke "LifeReduction" within another function.
 //How you do this depends on the condition, by colliding? raycast? or a loop?
 //Reduces Life with -1.
     Health.life -= 1;
     }

If you want to display the total amount of lives on the screen, then you can do that by using string method to GUIText. Or using a sprite as indicator for the total amount of lives you have.

If you don't know how to script or don't understand script that much. I highly recommend you to get some practice and some basic knowledge about how to script inside of MonoDevelop. I also began from scratch without help from professional tutors, learning from tutorials I found on the Unity site and Youtube.

Unity has an 28 episodes long tutorial, introducing you to basics of C# in Unity. https://unity3d.com/learn/tutorials/topics/scripting

After these 28 episodes I guarantee you, you will be able to make your own script with help of using the Unity Reference. And you will understand scripts a lot better.

Comment
Add comment · Show 2 · 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 rOBY GAMES · Jul 24, 2015 at 10:55 PM 0
Share

Thanks again Hexer. Try your script tomorrow, then let you know everything works itself. :-)

avatar image rOBY GAMES · Jul 27, 2015 at 06:17 PM 0
Share

Hello hexer. I finally solved !! I was all day editing scripts. The problem was. In the script Contovita. Thank you for your help. :-)

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

22 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

Related Questions

Regain health on GUI 2 Answers

I need help with the player's health points 1 Answer

Damage script is screwed up...? what to do? 1 Answer

c# how to change player location on 0 hp 1 Answer

Script Wont Take away health from my player when hit by enemy bullet. 0 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