Hi again! As the title says, I tried to make something by also trying to follow all the suggestions I received from previous posts, but I can’t seem to figure this out.
What are my mistakes? Am I making it the best way? Does my idea have sense? How would you do that?
The general idea: the player shoots a bullet, when it collides with the enemy sprite its scripts updates its health value from 100 to 75 (the base enemy health is 100 and the bullet damage is 25), this works as it should but i can’t understand how to connect the TMP text field that indicates the health to the value of the health of each enemy and make it update every time an enemy gets hit showing the value of health that updated in that moment. (I tried to be as clear as possible).
Here are my scripts:
using UnityEngine;
using TMPro;
using UnityEngine.UI;
// This script used to try to get the Enemy's health value to store it and send the updated value to the TMP.
// I even lost the track of what i was doing after two hours.
public class EnemyHealth : MonoBehaviour
{
[SerializeField]
private GameObject enemyGameObject;
private TMP_Text hText;
private EnemyAI eHealth;
void Start()
{
eHealth = enemyGameObject.GetComponent<EnemyAI>();
hText = GetComponent<TMP_Text>();
}
}
using UnityEngine;
using System.Collections;
public class Projectile : MonoBehaviour
{
public float speed = 100f;
public float lifeTime = 3f;
public float friction = 0.005f;
public float projectileDamage = 25f;
private IEnumerator coroutine;
[SerializeField] private Rigidbody2D rb;
// This contains the coroutine starts the spawning of the projectiles.
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.linearVelocity = transform.right * speed;
coroutine = ProjLifeTime(lifeTime);
StartCoroutine(coroutine);
}
// This calculates the friction of the projectile to make is smoothly slow down.
void Update()
{
rb.linearVelocity -= friction * rb.linearVelocity;
}
// This destroys the projectiles after 5 seconds.
private IEnumerator ProjLifeTime(float lifeTime)
{
yield return new WaitForSeconds(lifeTime);
Destroy(gameObject);
}
// This detects the collision with a GameObject that has the tag "Enemy" to destroy iself.
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Enemy"))
{
Destroy(gameObject); // Destroy the projectile itself
}
}
}
using System.Collections;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class EnemyAI : MonoBehaviour
{
public GameObject player;
public float speed = 5f;
private float enemyHealth = 100f;
public float EnemyHealth;
private Rigidbody2D rb;
// This finds the GameObject tagged with "Player".
void Start()
{
rb = GetComponent<Rigidbody2D>();
player = GameObject.FindWithTag("Player");
}
// This moves the enemy towards the player.
private void Update()
{
Vector2 direction = (player.transform.position - transform.position);
rb.linearVelocity = direction * speed;
}
// This detects when a GameObject tagged with "Projectile" hits it, then deducts some health points.
// When the health points reach 0, the enemy gets destroyed.
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Projectile"))
{
Projectile projectile = collision.gameObject.GetComponent<Projectile>();
{
EnemyHealth -= projectile.projectileDamage;
if (EnemyHealth <= 0)
{
Destroy(gameObject);
}
}
}
}
}
I added as much notes as I could and I tried to be as clear as possible with them.
I would greatly appreciate some help, I tried my best before coming here because I know that, as a beginner, I probably write and organize scripts very badly compared to what you might usually do, and I wanted to try to make it by myself, first of all, so I apology in advance.