Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Jamticity · Sep 18, 2013 at 09:49 PM · c#targetnew

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();
 }

}

Comment
Add comment · Show 2
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 robertbu · Sep 18, 2013 at 09:59 PM 0
Share

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.

avatar image Jamticity · Sep 19, 2013 at 11:03 AM 0
Share

thanks for your help im calling the activation of the componet after a the game starts works well

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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");
Comment
Add comment · Show 3 · 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 Jamticity · Sep 18, 2013 at 11:59 PM 0
Share

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

avatar image ArkaneX · Sep 19, 2013 at 07:53 AM 0
Share

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);
avatar image vexe · Oct 11, 2013 at 06:01 AM 0
Share

@Jamticity: If this answer helped you solve your problem, please give it the tick.

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

17 People are following this question.

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

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


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