Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Whiteleaf · Apr 28, 2016 at 06:42 AM · listfunctionparametersargumentexception

Function is being called even when not told to

I'll get to the chase, basically I have a box around my character, and whenever that box touches an enemy it adds to a list if it hasn't been added already. This is so that I can attack multiple enemies at once, and it works great.

But here's my problem, as soon as I kill an enemy only SOMETIMES does Unity throw and argument exception about the list being out of range from the index I'm using.

And I know exactly what's causing it, my enemy is dying before I even call the attacking function; which is strange because I haven't even called the function yet to make it take damage and potentially leading to death.

Here's my code:

Firstly, I go through a loop for all enemies that are in the list:

 if (canAttack && Enemies.Count > 0) {
 
                     for (int i = 0; i < Enemies.Count; i++) {
                         if (Enemies [i] != null) {
                             print ("enemy i is not null name is " + Enemies[i].name);
                             StartCoroutine (WaitForAttack (i, Enemies[i].transform));
                         } else {
                             print ("enemy i is null");
                         }
                     }
 
                 }


Next, I call the "WaitForAttack" coroutine that way the damage is "lined" (lack of better word) up with my attacking animation.

     IEnumerator WaitForAttack(int i, Transform IT)
     {
         print ("i name " + Enemies [i].name);
         yield return new WaitForSeconds (attackTime);
         if (Enemies.Contains (IT) == false) {
             print ("null " + IT.name);
 
         } if(Enemies.Contains(IT) == true) {
             //Enemy is dying before we call the attack function
             Attack (Random.Range (minDamage, maxDamage), Enemies [i]);
         }
     }

Unity also oddly enough still claims it has the enemy in the list, as you can see I'm checking. Also, it only happens after the WaitForSeconds() function.

Lastly, the attack function:

 public void Attack(int dmg, Transform obj)
     {
         if (called == false) {
             called = true;
 
             shaker.ScreenShake ();
 
             GameObject b = (GameObject)Instantiate (blood, obj.position, Quaternion.Euler (0, 0, Random.Range (-360, 360)));
 
             Vector3 s1 = new Vector3 (1f, 1f, 1f);
             Vector3 s3 = new Vector3 (1.5f, 1.5f, 1f);
 
             int r = Random.Range (1, 3);
 
             if (r == 1) {
                 b.transform.localScale = s1;
             }
             if (r == 2) {
                 b.transform.localScale = s3;
             }
 
             print ("Attack");
 
             GameObject t = (GameObject)Instantiate (attackText, obj.FindChild ("hitTextPos").position, Quaternion.identity);
             t.transform.FindChild ("hitText").GetComponent<Text> ().text = dmg.ToString ();
             Destroy (t, 2);
 
             EnemyHealth hp = obj.GetComponent <EnemyHealth> ();
 
             hp.Hurt (dmg);
 
             called = false;
         }
     }


You might tell me that it's something wrong with my WaitForSeconds() function, and you could be right, although as I've said this happens about 75% of the time, other times it executes the way it's intended to and doesn't throw up any errors.

Sorry if I'm a bit vague; I'm tired and frustrated. Ask any questions if you need to!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Whiteleaf · Apr 28, 2016 at 09:02 PM

I've opted to using the Find method in my list, thanks for the help though.

EDIT: It seems that the Contains method works better than Find and is simpler, it is now working without issues.

Comment
Add comment · 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
0

Answer by Bunny83 · Apr 28, 2016 at 07:02 AM

Well, the problem is most likely because you store the initial index of an enemy inside your list. When you remove an element from a List the indices of the following elements will change. You didn't posted any code where you remove elements from your List but it's the most likely case.

I don't see a reason why you actually use the index inside your "WaitForAttack" coroutine. The IT variable already references the enemy at that index.

Also your "called" variable seems pointless. The Attack method is not a coroutine, so since Unity runs the scripting engine in a single thread it's not possible to have multiple Attack methods run at the same time.

It might also be related to the way you actually fill your Enemies List. Since your coroutines delay the Attack action a few frames the List could be changed in the meantime,

Comment
Add comment · Show 1 · 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 Whiteleaf · Apr 28, 2016 at 07:31 AM 0
Share

The enemies are removed from the list when they die as well as when the player stops colliding with them. The "called" bool is because Unity was calling the attack function multiple times for no reason and it seemed to fixed it.

I'll try out your suggestions tomorrow, thanks for the input.

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

41 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

how to make function parameters don't need to declare (custom parameters)?? 1 Answer

How to make a game object of a certain class? 1 Answer

help with objexporter- passing parameters to C# function from JS 1 Answer

Storing functions in a list/array? 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