Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Peterpantsu · Mar 11, 2017 at 10:17 AM · arraylistdistancefor-loop

Using for loop to gather all waypoint objects near player

I'm having a problem with a list that I'm creating for all waypoints near the player. The total number of teleporters in the first list is 7, but the second list prints higher than that and eventually the index goes out of range. I've been at this for a few hours trying to figure out what is going wrong, but I'm drawing a blank.

Any ideas? I'd greatly appreciate any assistance.

 //Filling the teleporter list

 Teleporters = GameObject.FindGameObjectsWithTag("Teleporter");
         NumberOfTeleporters = Teleporters.Length;
 
 //in a Get Button Down if statement
  if (!Finding)
             {
                 FindNear();
                 Finding = true;
             }


 //This is the function that is supposed to gather all teleporter waypoints and add the waypoints within distance parameters into a new list. It then sets a child object on

  void FindNear()
     {
         for (int i = 0; i < NumberOfTeleporters; i++)
         {
             if (Vector3.Distance(CameraRig.transform.position, Teleporters[i].transform.position) < MaxRange &&
                 Vector3.Distance(CameraRig.transform.position, Teleporters[i].transform.position) > MinRange)
             {
                 NearTeleporters.Add(Teleporters[i]);
                 NearTeleporters[i].GetComponent<NearPortal>().Portal.SetActive(true);
             }
         }
         NumberOfNear = NearTeleporters.Count;
 
         Clearing = false;
     }
 
 
 //In a get button up if statement

           if (!Clearing)
             {
                 NearClear();
                 Clearing = true;
             }


 //Function that clears the second list and sets all child objects in that list as inactive
 
   void NearClear()
     {
         if (NearTeleporters[0] != null)
         {
             for (int i = 0; i < NearTeleporters.Count; i++)
             {
                 NearTeleporters[i].GetComponent<NearPortal>().Portal.SetActive(false);
             }
             NearTeleporters = new List<GameObject>();
 
             NumberOfNear = NearTeleporters.Count;
   
         }
 
         Finding = false;
     }



Comment
Add comment · Show 1
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 Scribe · Mar 11, 2017 at 12:04 PM 0
Share

Your index out of range probably comes from line 24, NearTeleporters[i] doesn't necessarily exist, you should probably meant Teleporters[i]

2 Replies

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

Answer by ExtinctSpecie · Mar 11, 2017 at 05:15 PM

@Scribe is right lets say that you add to the NearTeleporters.Add(Teleporters[i]); for indexes i = 0 , i =5 , and i = 6; for the second index i = 5 you're trying to access the 5th element of NearTeleporters when it only has 2 elements at that point and it throws an error NearTeleporters[i].GetComponent().Portal.SetActive(true); cant be executed

Comment
Add comment · 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 ExtinctSpecie · Mar 11, 2017 at 05:16 PM 0
Share

NearTeleporters[i].GetComponent().Portal.SetActive(true); change this line into : NearTeleporters[NearTeleporters.Count-1].GetComponent().Portal.SetActive(true); this way you can access the last element you added in the list

avatar image
0

Answer by Harardin · Mar 11, 2017 at 05:54 PM

You can try it like this this will alow you to find the smallest distance, also read something on Mathf and Vectors

         using System;
 
         GameObject[] yourObjectsArray = new GameObject[0] ; // Asign it first somwhere
         Vector3[] ObjectsCoordinates = new Vector3[0];
         float[] closestArr = new float[0];
         for (int i = 0; i < yourObjectsArray.Length; i++)
         {
             GameObject obj = yourObjectsArray[i]; 
             ObjectsCoordinates[i] = new Vector3(obj.transform.position.x, obj.transform.position.y, obj.transform.position.z);
             closestArr[i] = Vector3.Distance(transform.position, ObjectsCoordinates[i]);
         }
         float closest = Mathf.Min(closestArr);
         int arrIndexclosest = Array.IndexOf(closestArr, closest);
         GameObject closestObject = yourObjectsArray[arrIndexclosest];

Not the cleanest code but should work.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Sorting Javascript array by Distance 0 Answers

Affect every object in array. 1 Answer

What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers

A node in a childnode? 1 Answer

Copy values between two classes in two lists. 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