I have a slider that has a maximum value of 5. Everytime my gameObject "Car" collides with a gameObject "Cone," the slider should decrease value by 1. Currently, the slider is decreasing from 5 to 0 when a cone is hit. Would someone please be able to explain to me why I cannot get the value to decrease only by 1? Thanks in advance.
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class moveCar : MonoBehaviour
{
public GameObject car;
public static int movespeed = 100;
public GameObject road;
float offset = 1000f;
public Slider slider;
int health = 5;
public void start()
{
}
public void Update()
{
transform.position += (Vector3.forward * movespeed * Time.deltaTime);
if (Input.GetKey(KeyCode.RightArrow))
{
this.transform.Translate(Vector3.right * 1f);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
this.transform.Translate(Vector3.left * 1f);
}
}
// // Start is called before the first frame update
// void Start()
// {
// }
// // Update is called once per frame
// void Update()
// {
// }
//}
private void OnTriggerEnter(Collider col)
{
if(col.name == "roadTigger")
{
//roadCreator.createRoad();
Vector3 newPosition = new Vector3(0f, 0f, car.transform.position.z + offset);
Instantiate(road, newPosition, Quaternion.identity);
Debug.Log("road created");
}
if(col.CompareTag("Cone"))
{
slider.value = health;
health = health - 1;
Destroy(col);
}
}
}