Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Chaseriver1994 · Sep 20, 2020 at 06:02 AM · enemysetactivechild objectdeactivatetargetting

How to setActive(true) then false on enemy child gameobject based on closest enemy to player? (Closest Enemy Target System)

Overall Question: How do I get enemy's childgameobject(1) to be setactive(false) when the player finds a new closest enemy?

Details:

I've been attempting to do this for 2 days now and can't find any answers on here or youtube, google search or anything.

So basically the script below is almost done... Currently when my player's attack range is within range of enemy the enemy's childgameobject(2) will be setactive(true) and when out of that range it'll be set false.

I also have a childgameobject(1) that gets set true if that enemy is closest to player. This is also working...

However the real question here is how do I get that childgameobject(1) to be set active false when the player finds a new closest enemy? Currently it'll be set active when the player finds a new closest enemy that enemy will now have childgameobject(1) to be set active true and obviously that's not the intended effect I'd like. I do deactivate childgameobject(1) when it's out of attack range but would like it to deactivate whenever a new closest enemy has been found.

Please let me know if there is a better way to implement a targetting system. the objects are just circles that when are active are above the enemies.

============================================================================ Current Script Below ============================================================================

public class Player : MonoBehaviour {

bool EnemyMark = false;

protected override void Update() {

//Find closest enemy

         float distanceToClosestEnemy = Mathf.Infinity;
         FindEnemy closestEnemy = null;
         FindEnemy[] allEnemies = GameObject.FindObjectsOfType<FindEnemy>();

         foreach (FindEnemy currentEnemy in allEnemies)
         {
             float distanceToEnemy = (currentEnemy.transform.position - this.transform.position).sqrMagnitude;
             if (distanceToEnemy < distanceToClosestEnemy)
             {
                 distanceToClosestEnemy = distanceToEnemy;
                 closestEnemy = currentEnemy;
                 EnemyMark = true;
             }

             //Activates childgameobject 2 with Enemy mark based on attackRange
             if (Vector2.Distance(transform.position, closestEnemy.transform.position) <= attackRange)
             {
                 closestEnemy.transform.GetChild(2).gameObject.SetActive(true);
             }

             //De-Activates childgameobjects 1 & 2 with Enemy mark based on attackRange
             if (Vector2.Distance(transform.position, currentEnemy.transform.position) >= attackRange)
             {
                 currentEnemy.transform.GetChild(1).gameObject.SetActive(false);
                 currentEnemy.transform.GetChild(2).gameObject.SetActive(false);
             }
         }

         //Activates childgameobject 1 with Enemy mark based closest enemy
         if (EnemyMark == true)
         {
             closestEnemy.transform.GetChild(1).gameObject.SetActive(true);
         }
         //De-Activates childgameobject 1 with Enemy mark based closest enemy
         else
         {
             closestEnemy.transform.GetChild(1).gameObject.SetActive(false);
         }

         if (closestEnemy == null) { return; }

}

}

Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

139 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

Related Questions

Nearest Enemy To Team (Without Crowding!) 1 Answer

how to make an unlockable object (Pet) spawn throughout scenes after it gets unlocked? 1 Answer

Show only selected enemy's health bar 1 Answer

physics.OverlapSphere colliders 1 Answer

.SetActive (false) deletes gameObject but it's effects still affect the player 0 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