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 Hypnotoad0 · May 19, 2014 at 12:15 PM · arrayvariabletargetclosestpick

Pick the oldest object from an array.

I'm making a tower defense game and i need my towers to always prioritise enemies that are the closest to the end point. Each enemy has a "age" variable. I'm trying to create an array of enemies that are currently in tower's range. How do i make the tower pick the object with the biggest "age" variable?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by KevLoughrey · May 19, 2014 at 12:23 PM

Something like this should work:

 int oldestIndex;
 
 for (int i = 0; i < array.Count() - 1; i++) {
     if (array[i].age > array[oldestIndex].age) {
         oldestIndex = i;
     }
 }
 
 //Now attack the object at array[oldestIndex];
Comment
Add comment · Show 5 · 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 tw1st3d · May 19, 2014 at 01:15 PM 0
Share

Great answer, should work perfectly. +1

avatar image Hypnotoad0 · May 19, 2014 at 05:14 PM 0
Share

Can you explain whats happening in this code? Im trying to rewrite it in javascript into my turret code but i can't quite understand it.

avatar image Lo0NuhtiK · May 19, 2014 at 05:28 PM 0
Share

for(...huh??) --> http://www.youtube.com/watch?v=Z4fd2J7imgA

Comparing stuff --> http://www.youtube.com/watch?v=iV3wry4F1J0

Those might help you understand what @$$anonymous$$evLoughrey is showing you.

avatar image KevLoughrey · May 19, 2014 at 08:57 PM 0
Share

I'll try to explain as best I can, but this type of thing is usually easier to explain in code :D

You have an array of enemies, and each individual enemy can be accessed by its array index (its position in the array). For example, the first enemy is at array[0], the second at array[1], and so on. If you want to retrieve the age variable of the first enemy, you call array[0].age

So to find the enemy with the oldest age, we have to iterate through the whole array of enemies with a for loop, and check their ages off each other. This is why we use the variable oldestIndex. It stores the index position of the enemy with the largest age value.

The if statement checks to see if the age value of the currently iterated enemy is greater than that of the currently know oldest enemy, and if it is, that enemy becomes the oldest.

avatar image Hypnotoad0 · May 19, 2014 at 09:45 PM 0
Share

@$$anonymous$$evLoughrey http://answers.unity3d.com/questions/710787/how-to-add-colliders-to-an-array-and-pick-the-olde.html this is the exact problem im dealing with, im getting the "Array index is out of range" error. Also should i use list ins$$anonymous$$d of array? I've been reading a lot and everyone says its better in every respect. Thanks for your help.

avatar image
0

Answer by Graham-Dunnett · May 19, 2014 at 12:24 PM

Sort the array old-to-new every few frames, then choose the object at the front of the array.

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

23 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

Related Questions

How do i get my script to work? 2 Answers

Choose a Random Tag 1 Answer

Cycle through targets. 2 Answers

Change variable of ex2D Sprite using the component ExScreenPosition via code .js script 0 Answers

Problem when trying to save variable in another variable. 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