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 jjdoughboy · Sep 03, 2020 at 06:31 PM · uibuttonliveshealth

how would I set up a regain health ui button

in my tower defense game I want to have a ui button that player can press with a cool down that will add a extra live to the total live in the level for ex: player has one life left they click on the regain health button on the screen to go from one life to two lives and than the button has a 5 second cool down I don't know where to begin with this one as I'm fairly new with scripts so can somebody point in the right direction. I know how to make a button in unity and I know how the on click section works I just don't know how I would type up the script for it. if have scripts that have functions that attribute to my lives system if need to show that off let me know. I will show this script as it says once the enemy reaches that end of the waypoints to destroy the enemy game object (Itself) and reduce the lives number by one.

this is my enemy movement script it has my lives system in it under endPath

 using System.Collections;
 using UnityEngine;
 
 [RequireComponent(typeof(Enemy))]
 public class EnemyMovement : MonoBehaviour
 {
     private Enemy enemy;
 
     private Transform target;
 
     private int waypointIndex = 0;
 
     // Start is called before the first frame update
     void Start()
     {
         target = Waypoints.points[0];
         enemy = GetComponent<Enemy>();
     }
 
     // Update is called once per frame
     void Update()
     {
         Vector3 dir = target.position - transform.position;
 
         transform.Translate(dir.normalized * enemy.speed * Time.deltaTime, Space.World);
 
         if (Vector3.Distance(transform.position, target.position) <= 0.2f)
         {
             GetNextWaypoint();
         }
     }
 
     void GetNextWaypoint()
     {
         if(waypointIndex >= Waypoints.points.Length - 1)
         {
             EndPath();
             return;
         }
         waypointIndex++;
         target = Waypoints.points[waypointIndex];
     }
 
     void EndPath()
     {
         PlayerStats.Lives--;
 
         Destroy(gameObject);
 
         WaveSpawner.EnemiesAlive--;
     }
 }
 

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 Reid_Taylor · Sep 03, 2020 at 06:47 PM

While I am sort-of confused why you gave the enemy script and not the player script I'll try my best :).

So let's say your player script looks like this:

 public class Player : MonoBehaviour
 {
       public int lives = 10;
 
       public void Hurt(int amount)
       {
             lives -= amount;
       }
 
       public void Heal(int amount)
       {
             lives += amount;
       }
 }


I would morph this into this:

 public class Player : MonoBehaviour
 {
       public int lives = 10;
 
       private float healTimer = 0;
       public float timeBetweenHeals = 10;
       public float amountToHealOnClick = 1;
 
       private void Start()
       {
             healTimer = timeBetweenHeals;
       }
 
       private void Update()
       {
             if (healTimer > 0)
             {
                   healTimer -= Time.deltaTime;
             }
       }
 
       public void Hurt(int amount)
       {
             lives -= amount;
       }
 
       public void Heal(int amount)
       {
             lives += amount;
       }
 
       public void HealOnClick()
       {
             if (healTimer > 0)
                   return;
 
             lives += amountToHealOnClick;
             healTimer = timeBetweenHeals;
       }
 }


Then assign the HealOnClick() function to your button. And I should note that this doesn't do any visuals but if you wanted to you could add radial fill image and set its fill amount in update to healTimer / timeBetweenHeals.

Hope this helps!

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

248 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

Related Questions

Make a button selectable for controller but not interactable 0 Answers

Unity UI Button states 1 Answer

Buttons become invisible when changing color (on Button or on Image) in script -1 Answers

Unity 5.0.2f1 UI Button OnClick Function 6 Answers

Using listeners to complete code errors 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