- Home /
Boolean on if statement not working
When I try to damage an enemy with the if statement, the boolean "isAttacking" is not working, the collision is working fine
part of code with problem private void OnCollisionEnter(Collision collision) { if (collision.gameObject.CompareTag("Enemy") && IsAtacking) { EnemyHp.DeductHealth(damage); Debug.Log("contact"); } }
entire code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class attack : MonoBehaviour
{
public GameObject Sword;
public bool CanAttack = true;
public float AttackCooldown = 1.0f;
public bool IsAtacking = false;
[SerializeField] Transform swordbody;
[SerializeField] float attackRange;
public HealthEnemy EnemyHp;
[SerializeField] float damage = 10f;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (CanAttack == true)
{
SwordAttack();
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy") && IsAtacking)
{
EnemyHp.DeductHealth(damage);
Debug.Log("contact");
}
}
public void SwordAttack()
{
IsAtacking = true;
CanAttack = false;
StartCoroutine(ResetAttackCooldown());
}
IEnumerator ResetAttackCooldown()
{
StartCoroutine(ResetAtackBool());
yield return new WaitForSeconds(AttackCooldown);
CanAttack = true;
}
IEnumerator ResetAtackBool()
{
yield return new WaitForSeconds(1.0F);
IsAtacking = false;
}
}
That should in theory work, just try changing up the script a bit like :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class attack : MonoBehaviour
{
public GameObject Sword;
public bool CanAttack = true;
public float AttackCooldown = 1.0f;
public bool IsAtacking = false;
[SerializeField] Transform swordbody;
[SerializeField] float attackRange;
public HealthEnemy EnemyHp;
[SerializeField] float damage = 10f;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (CanAttack == true)
{
SwordAttack();
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy") && IsAtacking == true)
{
EnemyHp.DeductHealth(damage);
Debug.Log("contact");
}
}
public void SwordAttack()
{
IsAtacking = true;
CanAttack = false;
StartCoroutine(ResetAttackCooldown());
}
IEnumerator ResetAttackCooldown()
{
yield return new WaitForSeconds(AttackCooldown); //As the Attack cooldown is the same as the 'bool' cooldown
CanAttack = true;
IsAtacking = false;
}
}
NOTE : If you want to make the timings different for when the 'CanAttack ' and the 'IsAtacking' get triggered then you have to change the script a bit...
Answer by Caeser_21 · Mar 17 at 02:21 PM
Maybe try changing the places of the if statements like :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class attack : MonoBehaviour
{
public GameObject Sword;
public bool CanAttack = true;
public float AttackCooldown = 1.0f;
public bool IsAtacking = false;
[SerializeField] Transform swordbody;
[SerializeField] float attackRange;
public HealthEnemy EnemyHp;
[SerializeField] float damage = 10f;
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (CanAttack == true)
{
SwordAttack();
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy")
{
if (IsAtacking == true)
{
EnemyHp.DeductHealth(damage);
Debug.Log("contact");
}
}
}
public void SwordAttack()
{
IsAtacking = true;
CanAttack = false;
StartCoroutine(ResetAttackCooldown());
}
IEnumerator ResetAttackCooldown()
{
yield return new WaitForSeconds(AttackCooldown); //As the Attack cooldown is the same as the 'bool' cooldown
CanAttack = true;
IsAtacking = false;
}
}
Answer by grimreefer24601 · Mar 17 at 07:01 PM
Have you looked at the value of isAttacking in the debugger? Are you using the debugger or just log statements? You really should use the debugger for this kind of issue.
Your "If" statement is correct and should work. As long as both conditions are true. So the collider tag could be something other than enemy. You could make that easier to test by separating the "if" statements as Caeser_21 said. I'd add a log in both statements to make sure it hits both. Although it would be best to use the debugger then you can track the variables through the process.
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enemy")
{
Debug.Log("contact");
if (IsAtacking == true)
{
EnemyHp.DeductHealth(damage);
Debug.Log("attack");
}
}
}