Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jeremiahbais · Aug 08, 2017 at 10:26 PM · unity 5boolif statement

OnTriggerEnter not working properly in if satetment

 void onTriggerEnter (Collider col)
 {
 
     if (col.gameObject == enemy) 
     {
         enemyInRange = true;

     }
         
 }

 void onTriggerExit (Collider col)
 {

     if (col.gameObject == enemy) 
     {
         enemyInRange = false;
     }

 }





 void Update () {    // this is my update func.
     timer += Time.deltaTime;


         if (timer >= timebetweenattack  && enemyInRange  ) {  //This is where i get wrong, when i remove "enemyinrange" in if statement, i can damage enemy but  even if the enemy is out of range.

         if (Input.GetMouseButton (0)) {
             Melee ();
         }
     }    
 }

void Melee() { timer = 0f;

     if(enemyHealth.currentHealth > 0)
     {

         // ... damage the player.
         enemyHealth.TakeDamage (attackDam);
     }
 }

}

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 Cornelis-de-Jager · Aug 08, 2017 at 10:44 PM 0
Share

Ensure that the object you are colliding with is set to an Trigger. You can do that by selecting it from its collider behavior in the inspector

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Cornelis-de-Jager · Aug 08, 2017 at 10:48 PM

Try the following:

  1. If your target is not set to an Trigger do that.

  2. Use a Tag instead

      if (col.tag == "enemy") // Have to set the tags however
         {
           enemyInRange = true;
         }
    
Comment
Add comment · Show 1 · 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 jeremiahbais · Aug 08, 2017 at 10:53 PM 0
Share

i already did that. not working. if i remove enemyinrange in my if statement, it is working properly. but as i said i want to damage enemy if it is inside my sphere collider

avatar image
0

Answer by bobisgod234 · Aug 09, 2017 at 06:45 AM

Its OnTriggerEnter and OnTriggerExit, not onTriggerEnter and onTriggerExit. Note the capital O's

What is 'enemy'? How are you assigning it? I suspect you may have assigned the enemy prefab to it. The enemy prefab won't compare true to any instance in the scene. I would use Cornelis-de-Jager's suggestion and use tags. Make sure you have actually set the enemy's gameobject with the collider to have an "enemy" tag.

Also try adding Debug.Log statements to see where the problem is, like this:

  void OnTriggerEnter (Collider col)
  {
      Debug.Log("Entered trigger " + col.gameObject + " are they equal? " + (col.gameObject == enemy);
      if (col.gameObject == enemy) 
      {
          enemyInRange = true;
      }
  }

Also, your code will have an issue where if two enemies get in range, and one leaves, your code will incorrectly think that no enemies are in range. Try maintaining a count of enemies instead. change enemyInRange to an int, and replace "enemyInRange = true;" with "enemyInRange++;" and "enemyInRange = false;" with "enemyInRange--;".

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

137 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

Related Questions

bool changing on its own? 0 Answers

I can't figure out why this if statement isn't responding 1 Answer

Tracking Player x Position for different things to happen not working 0 Answers

I want to test in C # if a PC / laptop supports Bluetooth 0 Answers

How to make on/off-like gui-button? 3 Answers


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