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 Babilinski · Nov 09, 2011 at 01:15 AM · errortransformarrayselection

Argument is out of range in array

oksy so I am creating a script to shuffle close targets

here is my code

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Beta_targeting : MonoBehaviour
 {
     public Transform selectedTarget;
     public List<Transform> targets;
     
     void Start ()
     {
         targets = new List<Transform>();
     }
     
     public IList<Transform> FindEnemiesInSphere(float radius)
     { 
         Collider[] cols = Physics.OverlapSphere(transform.position, 10);
         SortedList<float, Transform> transforms = new SortedList<float, Transform>();
         foreach (Collider hit in cols) {
             if (hit && hit.tag == "Enemy"){                    
                 float Dist = Vector3.Distance(transform.position, hit.transform.position);
                 transforms.Add(Dist, hit.transform); 
             }
         }
         foreach(Transform trans in FindEnemiesInSphere(6.0f))
         {
             targets.Add(trans);
         }
         return transforms.Values;
     }
 
     void Update ()
     {
         if(Input.GetKeyDown(KeyCode.Tab))
             choosingtarget();
     }
     
     public void choosingtarget()
     {
         if(selectedTarget == null) {
             selectedTarget = targets[0];
         }
         else {
             int index = targets.IndexOf(selectedTarget);
             if(index < targets.Count - 1) {
                 index++;
             }
             else {
                 index = 0;
             }
             selectedTarget = targets[index];
         }
     }    
 }

however I get an error

"Argument is out of range. Parameter name: index"

any help?

Comment
Add comment · Show 3
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 Bunny83 · Nov 09, 2011 at 04:25 AM 1
Share

I've reformatted your code because it was really hard to see what belongs to what.

As you can see you don't call FindEnemiesInSphere anywhere except in the function itself!!!! This would lead to a Stack overflow because you would recursivly call the function. Good for you that you don't call the function at the moment, but it should be called (after you fixed it of course) or your targets - List will stay empty like syclamoth said.

avatar image syclamoth · Nov 09, 2011 at 04:34 AM 0
Share

I get the feeling that @Babilinski is fundamentally not understanding the point of that function (which I wrote earlier today, as a solution to a different problem). The bit in the middle which calls the function inside itself is an addition to my script, which kind of screws everything up!

avatar image Bunny83 · Nov 09, 2011 at 04:45 AM 0
Share

Ok, just to keep the information chain:

http://answers.unity3d.com/questions/184053/targeting-multiple-enemys.html

(i found it in your answers ;))

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by syclamoth · Nov 09, 2011 at 01:47 AM

The problem is that you never actually assign anything to 'targets'! Which means that index '0' will be out of range, because targets is empty. You need to have a line in there somewhere which goes like-

 targets = FindEnemiesInSphere(radius);

before you try to access any of its members.

Also, what is this line doing in your FindEnemiesInSphere function?

 foreach(Transform trans in FindEnemiesInSphere(6.0f))
 {
       targets.Add(trans);
 }

That really shouldn't be there. I don't think you understood what the point of that method was when I wrote it for you earlier.

Comment
Add comment · Show 5 · 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 Babilinski · Nov 09, 2011 at 01:54 AM 0
Share

the list targets does not get any new transforms. do you know why that is?

avatar image syclamoth · Nov 09, 2011 at 03:02 AM 0
Share

I just explained it! What part of my post can't you understand?

avatar image Babilinski · Nov 09, 2011 at 03:08 AM 0
Share

I understand all of it. When I hit play and hit the button it still says 0

avatar image syclamoth · Nov 09, 2011 at 03:42 AM 0
Share

Can you post your 'Start' function? The problem here is that it should be assigned instantaneously- if you check for enemies when there are none, and then look at the result of that check later on when there are enemies around, it'll still show zero enemies until you update it again! You need to put the

 targets = FindEnemiesInSphere(radius);

line in every time you want to use it.

avatar image Bunny83 · Nov 09, 2011 at 04:31 AM 0
Share

I guess the

 foreach(Transform trans in FindEnemiesInSphere(6.0f))

loop should be inside choosingtarget at the beginning. Also make sure you clear your targets list before adding your targets. If you call this function several times it would add-up the same targets multiple times and the targets list would grow until you run out of memory :D

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Send array of transforms via RPC. [C#] 1 Answer

Unity3D - Playback object array of position (with dynamic velocity) 0 Answers

"add selected" editor script 1 Answer

Getting radius around multiple transform 1 Answer

How to have a GameObject follow a path with c#? 3 Answers


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