- Home /
Auto-targetting script not working
I have this script that apparently isn't working. The goal of the script is to randomly select a target from an array of "allies" (or the player and player hirelings) in another script. What this script is supposed to do is go through each of the objects in the list of "allies", add the array location number to a local array (TargetsInLOS). This should result in a bunch of array location numbers in the TargetsInLOS, in which a random number from here is called and finally a return the number. The returned number should be the array location from the "Allies" list that is in the line of sight of the enemy.
Hopefully I commented enough to show my thinking / reasoning
int GetTargetSelection() {
int[] TargetsInLOS = new int[0]; // List of the target selections that will be filled with selection variables in line of sights
int selection2 = 0; // Selection for each of the gameobjects for the "ForEach" part
foreach (GameObject item in EntitiesDataBase.Allies) {
// The script below works, all it does is set LOS to true if there is a line of sight between the two vector3 positions
CalculateLineOfSight(transform.position, item.transform.position);
if (LOS == true) {
// This works on another script, so hopefully this isn't the problem, but just incase...
// Stores the current variables of TargetInLOS into a storage array
int[] Storage = TargetsInLOS;
// Sets the TargetInLOS to a new array with a length of it's original length + 1
int var1 = TargetsInLOS.Length + 1;
TargetsInLOS = new int[var1];
//Replacing back the variables from storage into TargetInLOS
int selection = 0;
foreach (int item1 in Storage) {
TargetsInLOS.SetValue(item, selection);
selection += 1; // Setting it so the next item will go into the next array insert
}
TargetsInLOS.SetValue(selection2, TargetsInLOS.Length - 1); // Setting the final value to the selection of the original item that has a line of sight
}
selection2 += 1; // Setting is so the next item will go into the next array insert
}
int selection1 = Random.Range(0, TargetsInLOS.Length); //Picks a random number from the selection provided
return (TargetsInLOS[selection1]); // Returns the selection of a random target that is "seen" by the enemy, which is used for aquiring the gameobject
}
I don't really know anything Too Too advanced, and I don't really feel like changing the allies array to a list, as I already have it "infused" with all my other scripts
this script may actually be in working order, I found later that it was the Line of Sight calculator that was only working for just the player and not any other entitiy, so that was changed...
So far it seems to work... the fine tuning now just needs to be done to the line of sight calculations...
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Failed setting triangles in my mesh 1 Answer
new Mesh() works outside of array, but not in array. Huh? 3 Answers
Vector3 resultant array sorting 2 Answers