• 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 Henrique · Aug 18, 2010 at 06:14 PM · gameobjectarrayenemytargetfind

FindClosestEnemy() change target?

Hi, i'm using the function I found in Unity Script Reference, about FindGameObjectWithTag.

Then I must be able to change between the targets tagged as "Enemy". How would i do in t$$anonymous$$s code?:

function FindClosestEnemy () : GameObject {

var gos : GameObject[]; gos = GameObject.FindGameObjectsWithTag("Enemy"); var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position;

for (var go : GameObject in gos) { var diff = (go.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = go; distance = curDistance; } if (Input.GetButton("Jump")) { //What would i do here??? how? } } return closest;

}

The problem is that it is an array, and doesn't looks like an array, so then i don't know how to increment "gos[THIS]"....

Thank you so much! :D

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by skovacs1 · Aug 18, 2010 at 06:37 PM

Your question made sense right up until the end. It is an array and looks like one just fine. You can't increment a gameobject and you already are iterating through the array if that's what you were trying to convey.

What you've got there is a foreach loop. It iterates through every element in the array. it is essentially equivalent to

for (var i = 0; i < gos.length ; i++)
{
    var go = gos[i];
    if(go)
    {
        //...the contents of your foreach loop
    }
}

Your calculation of the closest enemy is fine except for the last if statement. Why are you trying to do somet$$anonymous$$ng when you jump for every loop iteration? The if statement should not be inside your function at all let alone t$$anonymous$$s loop.

I t$$anonymous$$nk you want somet$$anonymous$$ng like:

function FindClosestEnemy () : GameObject { var gos : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy"); var closest : GameObject; var distance = Mathf.Infinity; var position = transform.position;

 for (var go : GameObject in gos) { 
     if((go.transform.position - position).sqrMagnitude &lt; distance) { 
         closest = go; 
         distance = curDistance; 
     } 
 }
 return closest;  

}

function Update() { var target : GameObject = FindClosestEnemy(); if (Input.GetButton("Jump")) { //Do whatever you want here. }

}

Comment
Add comment · Show 2 · 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 Henrique · Aug 18, 2010 at 06:52 PM 0
Share

"Jump" is only for test. It is a test project in Unity. My question is how would i use the button "Jump" to change between the enemies...from nearest to far enemies.... I want only change the target... How?

Thx :D

avatar image skovacs1 · Aug 24, 2010 at 06:45 AM 0
Share

I never asked about "Jump" or whether it is a test - that has no bearing on anything. I don't see where you are having difficulty with the answer. There is a function to find the closest enemy right here that will work for you and if you want to do that when they push jump, then do so - it's not hard: function Update(){if (Input.GetButton("Jump")) target = FindClosestEnemy();} If you don't want to find the closest enemy, then don't ask about FindClosestEnemy(). If you want to simply find the next-closest-target, then I would ask that you revise your question to ask that.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

how to find the closest (and second closest) object with tag - one object 4 Answers

add waypoint to transform[] 2 Answers

Look At Target? 2 Answers

instantiating objects problem 1 Answer

Search for specific entity in an array? 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