- Home /
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;
}
Your index out of range probably comes from line 24, NearTeleporters[i]
doesn't necessarily exist, you should probably meant Teleporters[i]
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
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
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.