- Home /
c# help fix argument is out of range error
i cant seem to figure out what i did wrong if i drag the targetting script on the character it works but if i start the game with the script pre attached to the player character i get the error
i'm getting this error when i press play and press tab at any distance
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.Transform].get_Item (Int32 index) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System.Collections.Generic/List.cs:633) Targetting.TargetEnemy () (at Assets/_Scripts/Targetting.cs:52) Targetting.Update () (at Assets/_Scripts/Targetting.cs:85)
targeting script
/// /// TargetMob.cs /// Oct 20, 2010 /// Peter Laliberte /// /// This script can be attached to any permanent gameobject, and is responsible for allowing the player to target different mobs that are with in range /// using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Targetting : MonoBehaviour { public List targets; public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
public void AddAllEnemies() {
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
public void AddTarget(Transform enemy) {
targets.Add(enemy);
}
private void SortTargetsByDistance() {
targets.Sort(delegate(Transform t1, Transform t2) {
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
}
//if we do not have an enemy targeted ywt, then find the clostest one and target him
//if we do have an enemy targeted, then get the next target
//if we have the last target in the list, then get then first target in the list
private void TargetEnemy() {
if(selectedTarget == null) {
SortTargetsByDistance();
selectedTarget = targets[0];
}
else {
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count - 1) {
index++;
}
else {
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
}
SelectTarget();
}
private void SelectTarget() {
selectedTarget.renderer.material.color = Color.red;
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
pa.target = selectedTarget.gameObject;
}
private void DeselectTarget() {
selectedTarget.renderer.material.color = Color.white;
selectedTarget = null;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab))
TargetEnemy();
}
}
As a guess, are you dynamically creating the enemies? If so, it is likely that AddAllEnemies() will not find any enemies resulting in an empty targets list. With an empty list, an index of 0 will generate an out of range error.
thanks for your help im calling the activation of the componet after a the game starts works well
Answer by ArkaneX · Sep 18, 2013 at 10:03 PM
The problem is in line
selectedTarget = targets[0];
It looks like your targets List is empty at that point. You're filling it in the Start method, but are you sure that any game object with "Enemy" tag is available at this point? Because if there are none, then
AddTarget(enemy.transform);
line won't be called. To determine if any enemy object was found, please put
print("enemies found: " + go.Length);
just after
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
hi thanks well i know that my enemy spawns but i think its calling the command beofr the moster spawns making it think theres no enemys how would i call this properly
You should add a target to a list just after it spawns. If your spawning script is attached to the player as well, then you should just use
var spawnedEnemy = (GameObject)Instantiate(.....);
GetComponent<Targetting>().AddTarget(spawnedEnemy.transform);
If, on the other hand, the script is attached to a different object, then you have to find your player first, then retrieve Targetting component, and finally add target:
var spawnedEnemy = (GameObject)Instantiate(.....);
var player = GameObject.FindWithTag("Player"); // or player could be kept as local variable
player.GetComponent<Targetting>().AddTarget(spawnedEnemy.transform);
@Jamticity: If this answer helped you solve your problem, please give it the tick.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
i need help removing an error in my targetting/attack script 1 Answer
how can i auto target Gameobject on startup 1 Answer
Destroy Gameobject once 0 health 2 Answers
c# destroy Gameobject on 0 hp 1 Answer