Damage with Bullets
I'm making a 2d Platforming Adventure Game. And I'm using test bullets to see if the boss could take damage. Here are the scripts for both. Projectile Controller: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ProjectileController : MonoBehaviour { public float speed;
public Player player;
public BossController boss;
public int damageToGive;
// Start is called before the first frame update
void Start()
{
boss = GetComponent<BossController>();
player = FindObjectOfType<Player>();
if (player.transform.localScale.x < 0)
speed = -speed;
}
// Update is called once per frame
void Update()
{
GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);
}
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Enemy")
{
Destroy(gameObject);
BossHealth.DamageBoss(damageToGive);
}
}
And Boss Health
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class BossHealth : MonoBehaviour { public int maxHealth;
public static int Health;
// Start is called before the first frame update
void Start()
{
Health = maxHealth;
}
// Update is called once per frame
void Update()
{
}
public static void DamageBoss(int damageToGive)
{
Health -= damageToGive;
}
The bullets disappear when they hit the boss but it doesn't damage the boss when I look at the integer in its inspector.
You're destroying the bullet gameobject before calling the boss damage method. Damage the boss first. --- Edit : $$anonymous$$aybe anyway. Been so long since I messed with any of this stuff I can't remember if it will go ahead and finish the method on that object or not.