- Home /
Question by
RVDL_IT · Apr 02, 2018 at 06:54 PM ·
c#gameobjectontriggerenterbooleanint
GameObject variable doesn't get filled in
I have a script that deal damage to an enemy. It takes the object that collides with the sword and lowers the health variable of the enemy's script. For some reason the hit boolean (ComboHit) (used for combo's and knockback) and the enemy GameObject only get filled in during some of the hits, but not all according to the Unity Editor window. Even weirder is that the enemy's health doesn get lowered and the enemy does get knocked back, although the amount of knockback is different every time. This shouldn't be an issue seeing as stuff isn't affected, but I'd like to solve it anyway before it gets in the way.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamagePlayer : MonoBehaviour {
//GameObjects
public GameObject Player;
public GameObject Enemy;
public GameObject Weapon;
//Booleans
public bool NormalAttack;
public bool ChargedAttack;
public bool DashAttack;
public bool ComboHit;
//Floats
public float EnemyInv;
void Update() {
//Hit Boolean Declaratioms
NormalAttack = Player.GetComponent<AttackControls>().NormalAttack;
ChargedAttack = Player.GetComponent<AttackControls>().ChargedAttack;
DashAttack = Player.GetComponent<AttackControls>().DashAttack;
Weapon = Player.GetComponent<AttackControls>().Weapon;
//Enemy Hit Cooldown
if(EnemyInv > 0) {
EnemyInv -= Time.deltaTime;
}
if(EnemyInv < 0) {
EnemyInv = 0;
}
if(EnemyInv == 0) {
// Enemy = null;
// ComboHit = false;
}
}
void OnTriggerStay2D(Collider2D EnemyChar) {
if(EnemyChar.gameObject.tag == "Enemy") {
Enemy = EnemyChar.gameObject;
ComboHit = true;
//Damage Dealings
if(EnemyInv == 0) {
if(DashAttack == true) {
Enemy.gameObject.GetComponent<HealthEnemy>().EnemyHealth-= 8;
}
if(NormalAttack == true) {
Enemy.gameObject.GetComponent<HealthEnemy>().EnemyHealth-= 5;
EnemyInv = 0.1f;
}
if(ChargedAttack == true){
Enemy.gameObject.GetComponent<HealthEnemy>().EnemyHealth -= 8;
EnemyInv = 0.1f;
}
}
}
}
}
Comment