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--;
}
}