So basically I have a script which when the user press a button it sumons one cat to help the main player the collect as much fish as possible. The way this works is that I found this script on Unity answers, created by Iceblitzyt. the way the script works is that the script target the nearest tagged enemy. My problem is is that after when the cats targets the closest fish I get this error:
ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index
I was wondering if there is a way of making the cats spawns to wonder around the area within camera view until the next fish has spawned in. Is it possible, thank you! :)
The script:
public List Enemies;
public Transform SelectedTarget;
private int scoreValue = 1;
void Start ()
{
SelectedTarget = null;
Enemies = new List();
AddEnemiesToList();
}
public void AddEnemiesToList()
{
GameObject[] ItemsInList = GameObject.FindGameObjectsWithTag("Fish");
foreach(GameObject _Enemy in ItemsInList)
{
AddTarget(_Enemy.transform);
}
}
public void AddTarget(Transform enemy)
{
Enemies.Add(enemy);
}
public void DistanceToTarget()
{
Enemies.Sort(delegate( Transform t1, Transform t2){
return Vector3.Distance(t1.transform.position,transform.position).CompareTo(Vector3.Distance(t2.transform.position,transform.position));
});
}
public void TargetedEnemy()
{
if(SelectedTarget == null)
{
DistanceToTarget();
SelectedTarget = Enemies[0];
}
}
void Update ()
{
TargetedEnemy();
float dist = Vector3.Distance(SelectedTarget.transform.position,transform.position);
//if(dist <150)
//{
transform.position = Vector3.MoveTowards(transform.position, SelectedTarget.position, 20 * Time.deltaTime);
//}
}
↧