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 06:38 PM · errorcolliderarraytriggeradd

How to add colliders to an array, and pick the oldest one?

Im trying to make a tower defense game and i need a tower targetting system that picks the oldest object (the enemy closest to the end) in its range and shoots at it until the object dies, then picks a new target. Each enemy object has a "age" variable that's its age in seconds.

How do i add the enemies in range to my enemiesinrange array? I need it to return the oldest object so that the turret can target it.

The following part doesn't compile:

 if(enemiesinrange.length > 0) {
         var oldestEnemy = enemiesinrange[0];
         var age = Collider.age;
 
         for (var i = 0; i < array.Count() - 1; i++) {
     if (array[i].age > array[oldestIndex].age) {
         oldestIndex = i;
     }
 }
 
         print(oldestEnemy);
     }

Here's the code of the entire function:

 function OnTriggerStay (other : Collider) {
 
 if(other.gameObject.CompareTag("Enemy")){
 selectedTarget = other;
 isidle=0;

 if(enemiesinrange.length > 0) {
         var oldestEnemy = enemiesinrange[0];
         var age = Collider.age;
  
         for (var i = 0; i < array.Count() - 1; i++) {
     if (array[i].age > array[oldestIndex].age) {
         oldestIndex = i;
     }
 }
  
         print(oldestEnemy);
     }
 }
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
0
Best Answer

Answer by MakeCodeNow · May 19, 2014 at 11:20 PM

It's maybe easiest to just sort the list. Something like this:

 Array.Sort(enemiesinrange, delegate(MyEnemyType a, MyEnemyType b)
 {
     if(a.age > b.age)
         return 1;
     else if(a.age < b.age)
         return -1;
     else
         return 0;
 });
 var oldestEnemy = enemiesinrange[0];
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 Hypnotoad0 · May 20, 2014 at 11:34 AM 0
Share

I need to add the colliders to the enemiesinrange array, ill make use of your sorting code after I do that.

var enemiesinrange = new Array();
var objects : int = 0;

 function OnTriggerEnter (other : Collider) {
 
 objects++;
 enemiesinrange.Add(Collider);
 print(other.BasicEnemy.age);
 print(enemiesinrange[0].BasicEnemy.age);
 }

This is my current code, its supposed to add the collider to my enemiesinrange. It returns an error "Object reference not set to an instance of an object" on the following line:

 enemiesinrange.Add(Collider);
avatar image MakeCodeNow · May 20, 2014 at 06:06 PM 0
Share

That means that enemiesinrange is null. You probably are not assigning it to anything when you declare it. It probably needs an = new List() or new SomeType[] or something like that. I'm not sure because you don't show the declaration here, but that's likely the problem.

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

21 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

Related Questions

Adding a collider to array 1 Answer

Can't click gameobject when over another trigger? 1 Answer

Script error: OnTriggerEnter 1 Answer

Check for rigidbody presence when in collision with a trigger 2 Answers

Collision returning an error 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