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
1
Question by PrimeDerektive · Dec 02, 2010 at 02:53 PM · arraytriggernull

How to do a check if an Array is empty?

I have a basic melee system implemented with a large cube trigger collider attached to the front of my player that gets enabled briefly on Input Fire1. My problem was, I only wanted it to be able to hit one person (it was calling the ApplyDamage function on all enemies in the cube in front of the player, even though I set it to deactivate itself after sending the ApplyDamage message in OnTriggerEnter).

So I came up with a simple solution I was rather proud of :) I created an Array called enemies, and add the enemy GameObject to the Array OnTriggerEnter. The in LateUpdate, I check if enemies[0] is null, and if not, call the ApplyDamage function on enemies[0] and clear the Array.

However, it throws an error when I check if enemies[0] == null, saying that "ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count." Is there another way I can check if the enemies Array is empty?

Here's my code:

private var enemies = new Array();

function LateUpdate(){

 if(enemies[0] != null){
     enemies[0].GetComponent(EnemyStatus).ApplyDamage();
     enemies.Clear();
 }

}

function OnTriggerEnter (other : Collider) {

 if(other.gameObject.tag == "Enemy"){
     enemies.Add(other.gameObject);
     gameObject.active = false;
 }


}

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
11
Best Answer

Answer by Anton S. · Dec 02, 2010 at 03:22 PM

Try this:

if(enemies.length > 0){
        enemies[0].GetComponent(EnemyStatus).ApplyDamage();
        enemies.Clear();
}

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 PrimeDerektive · Dec 02, 2010 at 03:26 PM 0
Share

Beautiful! Thanks a bunch my man.

avatar image
1

Answer by Mince_AT_FC · Dec 08, 2015 at 11:59 AM

@Anton S Wouldnt it be ....

if(enemies.length > -1){ }

as enemies[0] is a valid index. -Mince-

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 ilmario · Mar 10, 2017 at 11:24 AM 1
Share

Nope.

enemies.length <-- this value is not the index, it's what can be expected : amount of items in the array. If it's 0 it's an empty array.

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

1 Person is following this question.

avatar image

Related Questions

Removing null object from array in unity 0 Answers

Populate an array of GameObjects with collision? 1 Answer

String in array is null when checking length, help bug?! 2 Answers

Avoiding adding duplicate objects to ArrayList 2 Answers

Why is my C# array of objects fully populated when some of them should be null? 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