Question by
Swardkhou · May 26, 2017 at 12:20 PM ·
c#scripting problem
How I make a damage to the player without using a colliderEnter2D?
I am doing a 2D videogame and the enemy throw a bullet, when it finished his animation, it makes a damage to the player and it is represented in the life bar. But I have a problem between the EnemyBullet script and HealthPlayer script, because the player do not receive damage and the life bar do not change. Could anybody help me, please?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
public int life;
public GameObject lifeUI;
// Use this for initialization
void Start()
{
}
void Update()
{
}
public void getDamage(float damage)
{
life -= (int)damage;
if (life >= 0)
{
life--;
lifeUI.GetComponent<Image>().fillAmount -= 0.1f;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyBullet : MonoBehaviour {
public GameObject obj;
private PlayerHealth player;
private float damage = 1f;
private float timeToKill = 2f;
private float initTime;
public void Start() {
player = obj.GetComponent<PlayerHealth>();
initTime = Time.time;
}
private void Update() {
if(Time.time >= initTime + timeToKill) {
Destroy(gameObject);
player.getDamage(damage);
}
}
private void OnTriggerEnter2D(Collider2D collision) {
Debug.Log("Shot");
Destroy(gameObject);
}
}
Comment
Your answer

Follow this Question
Related Questions
The laser problems 0 Answers
Add 2D Skeleton to the Scene if Button was pressed 0 Answers
Integer arrays not comparing properly. 3 Answers
How to remove an item from a list of custom variables 1 Answer