I have a little question, probably really straight forward but I’m still very much a beginner and just struggling a little with this.
I have 2 enemies that spawn and i am using a script attached to my projectile to destroy the projectile and the other object it hits using the following script
public void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
Destroy(other.gameObject);
FindObjectOfType<Score>().IncreaseScore();
}
what I am trying to do is when one of the enemy types is destroyed i want to increase the players health. I have the players health decrease when the enemies collide so all the info is there, just now sure how to implement it.
First of all use Destroy(gameObject); at the end. Code Execution stops once gameobject is destroyed. Get The reference to player just like you did for score FindObjectOfType().IncreaseHealth();
Thanks for reply, so I’ll just need to make another script to handle health increase then call upon here, how would I make it so only one of my 2 enemy prefabs increase health, as I think this script will increase health on every enemy killed. Will i just need to do a another script to split them up?
I’ve managed to get my health to increase on a key press just now which is a step in the right direction, will slowly get there from that.
Thanks for replies, the community here is fantastic, especially for a noob like me. I’m really enjoying Unity and what I’ve managed to learn and create in such a short time. Looking forward to diving deeper into the world C# and continuing to improve.
I’m still driving myself mad trying to figure this out, I’ve got as far as my health increasing when I press the F key, which is progress I guess.
I have a player that has a Controller Script and a Damage Script, the damage script is where I’ve put the health increase, this is also where the player damage and death is handled.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;
using UnityEngine.UI;
public class DamagePlayer : MonoBehaviour
{
public float playerHealth = 500f;
public float currentHealth;
public float damage = 0.4f;
public float healthUp = 50f;
public GameObject redEnemy;
void Start()
{
print(playerHealth);
}
public void FixedUpdate()
{
if (Input.GetKeyDown(KeyCode.F))
{
playerHealth += healthUp + currentHealth;
}
}
void OnCollisionStay(Collision collision)
{
if (collision.gameObject)
{
playerHealth -= damage;
print("Im HIT" + playerHealth);
}
//Activating public bool IsDead function when player IsDead = True
if (IsDead() == true)
{
playerHealth = 0;
Destroy(gameObject);
ActionAfterDelay.Create(2.5f, () => { SceneManager.LoadScene("GameOver"); });
}
}
public bool IsDead()
{
//Player health less than 1 player is dead = true
return playerHealth < 1;
}
}
I have a Spawn Manager Script which spawns 2 enemies, A basic (white) and a large (red). The player fires a projectile that has a script to deal with destroying the projectile and the enemy it hits.
What I’m really struggling with is how to detect if I’ve killed the red enemy and make my health increase instead of pressing a key. I’ve managed to build this whole little game from scratch the past week and this is the only little thing that is holding me back. I’m sure to the trained eye the answer is simple but I’m completely baffled. I’ve looked at many a thread and video and seen many ways to implement health systems, and but just struggling to get anything working on this.