Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by doggo007 · Mar 17 at 01:22 PM · collisionif-statementsbooleans

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;
     }
 }
 

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Caeser_21 · Mar 17 at 01:31 PM 0
Share

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...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
      }
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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");
               }        
           }
       }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

202 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Question Regarding If Statement Errors, Booleans 1 Answer

Scavenger Hunt List Bools Question 1 Answer

Bool statement returns true, while conditions is false in IF statement 1 Answer

Only detect collision once, if statement. C# 1 Answer

Detect if a variable is a Boolean? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges