• 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
Question by ChrisSch · Mar 07, 2014 at 02:29 PM · arraylistpathfindingfor loopfor

Generic list of objects, then distance to those objects? (js)

Hi, I'm working on my own pathfinding solution, for learning purposes, and the only tutorial I understood (mainly because it was in javascript not C#) was Eteetski's AI pathfinding, but It gives me an error even tho I copied it to the letter.

Anyway, ofc I ran into a problem as soon as I started. xD I read that arrays are slow and don't have type casting, builtin arrays, which I always used for everything so far are the fastest, but can't be resized or added to, so I ended up with generic lists being the answer as faster than arrays.

Now I've never ever worked with lists before so I'm not quite sure how to handle them especially in for loops.

What I want to do is get a list of all gameObjects or transforms (not sure yet), on trigger enter. And that part I managed. But now I want to get the distance of all those objects to another object, the enemy, and put it in another list, but the way I have it set up now it only returns 2 values when there should be 4 since there's 4 in the initial list.

Here's the code I have so far:

 #pragma strict
 import System.Collections.Generic;
 
 var doors : List.<Transform>;
 var doorToEnemyDistance : List.<float>;
 
 function OnTriggerEnter(trig : Collider)
 {
     if(trig.gameObject.tag == "AIPathDoor")
     {
         doors.Add(trig.transform);
         Debug.Log(doors.Count);
     }
     if(trig.gameObject.tag == "Enemy")
     {
         for (var i = 0; i < doors.Count; i++)
         {
             var distance = Vector3.Distance(trig.gameObject.transform.position, doors[i].position);
             doorToEnemyDistance.Add(distance);
         }
     }
 }
Comment

People who like this

0 Show 0
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

Answer by YourGamesBeOver · Mar 07, 2014 at 03:12 PM

Without seeing your scene, its really hard to figure it out. try adding Debug.Log(doors.count); to your if(trig.gameObject.tag == "Enemy") block. Perhaps your loop is only running twice, in which case the problem is somewhere else (perhaps the other if statement?). your code seems fine...

Comment

People who like this

0 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 ChrisSch · Mar 07, 2014 at 03:24 PM 0
Share

Strange, the debug.log in "if(trig.gameObject.tag == "AIPathDoor")" returns 1, 2, 3, 4, and in the "Enemy" for loop it only returns 2, 2.

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.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

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

Pathfinding through pairs of connections 2 Answers

For all elements above a certain index 1 Answer

A node in a childnode? 1 Answer

What am I overlooking? -1 Answers

How to make a List or Array of functions with type Void 4 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