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 CallumWB · Nov 08, 2016 at 03:42 PM · c#newbiehealth

Checking for a float as a health bar

Hey all I am new to coding and I am working on a new level I have used this code to show on screen the players health. Is there a way where when the health hits no Health that I respawn at a set location? any help would be great thanks all in advance

using UnityEngine; using System.Collections;

public class Health : MonoBehaviour {

 GameObject box; //variable collectable box 
 public float health = 100; // variable for score floated to the screen/publically

 //use this for initialization
 void start()
 {
     box = GameObject.FindWithTag("Health");
 }

 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Health")
     {
         health -= 5;
         Destroy(other.gameObject);

     }
 }

 void OnGUI()
 {
     GUI.Label(new Rect(150, 160, 300, 300), "Health : " + (int)health);
 }
 void Update()
 {

 }

}

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

2 Replies

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

Answer by Makri907 · Nov 08, 2016 at 04:21 PM

Yes, you can do something like this:

      GameObject box; //variable collectable box 
         //Add a transform for your spawn position
         Transform spawn;
 //Add height you want to spawn at
 public float spawnHeight;
          public float health = 100; // variable for score floated to the screen/publically
         //Add variable to save the initial health
         float savedHealth;
          //use this for initialization
          void start()
          {
         //Assign health to saveHealth
         saveHealth = health;
         //Find the spawn object
         spawn = GameObject.Find("Spawn").GetCompnent<Transform>();
              box = GameObject.FindWithTag("Health");
          }
          void OnTriggerEnter(Collider other)
          {
              if (other.tag == "Health")
              {
                  health -= 5;
                  Destroy(other.gameObject);
              }
          }
          void OnGUI()
          {
              GUI.Label(new Rect(150, 160, 300, 300), "Health : " + (int)health);
          }
          void Update()
          {
         //Check if health is zero
         if(health<=0){
         Vector3 pos = new Vector3(spawn.position.x,spawn.position.y,spawn.position.z);
 pos.y = spawn.position.y + spawnHeight;
     transform.position = pos;
         //Reset Health
         heath = saveHealth;
         }
      }
     }


Tell me if this works. I am not at my computer so I can't test it myself.

Note: Make a gameObject in the editor and name it Spawn or whatever you say it is in, spawn = GameObject.Find("[insert your gameobject name]").GetComponent();

Comment
Add comment · Show 4 · 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 CallumWB · Nov 22, 2016 at 01:22 PM 0
Share

Yeah it works fine thanks loads

avatar image CallumWB · Nov 22, 2016 at 04:28 PM 0
Share

Hey I looked at the code and it seamed okay I have just put it to use and I am getting an error image as you can see what unity is saying rather than me trying to explain the problem its talking about the name space will upload the image when I get home

avatar image Makri907 CallumWB · Nov 22, 2016 at 06:18 PM 0
Share

I tried this code and didn't get an error:

 GameObject box; //variable collectable box 
         //Add a transform for your spawn position
         public Transform spawn;
         //Add height you want to spawn at
         public float spawnHeight;
         public float health = 5; // variable for score floated to the screen/publically
         //Add variable that will be used to reset health
         public float savedHealth;
         //use this for initialization
         void start()
         {
             //Find the spawn object
             box = GameObject.FindWithTag("Health");
         }
         void OnCollisionEnter(Collision other)
         {
             if (other.collider.tag == "Health")
             {
                 health -= 5;
                 Destroy(other.collider.gameObject);
             }
     
         }
         void OnGUI()
         {
             GUI.Label(new Rect(150, 160, 300, 300), "Health : " + (int)health);
         }
         void Update()
         {
             if (health <= 0) {
                 SpawnPlayer ();
             }
     
         }
         void SpawnPlayer(){
             if (health != 0) {
                 return;
             }
             Vector3 pos = new Vector3 (spawn.position.x, spawn.position.y, spawn.position.z);
             pos.y = spawn.position.y + spawnHeight;
             transform.gameObject.GetComponent<Rigidbody> ().transform.position = pos;
             //Reset Health
             health = savedHealth;
     
         }
     }

Note: Just assign the spawn object, the savedhealth, the health, and spawn height, in your inspector. Savedhealth should be the value you want your health to be when your player re-spawns.

avatar image Makri907 Makri907 · Nov 22, 2016 at 06:21 PM 0
Share

maybe you left out the headers: using UnityEngine; using System.Collections; public class Health : $$anonymous$$onoBehaviour { } Note: the code should be nested in the class.

avatar image
0

Answer by CallumWB · Nov 22, 2016 at 01:22 PM

Sorry for the slow reply been busy with College work

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

256 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 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 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 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 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 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 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 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 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 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 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 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

Do you need to have specific script types for different mobile platforms? 1 Answer

Making a potion class (change values two objects down) 0 Answers

NullReferenceException by detecting health 1 Answer

What is wrong with my code...watched an unity tutorial and I keep forgetting everything! 1 Answer

Hearth based health system help 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