Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 manders · Feb 12, 2014 at 10:17 AM · errorlist

Argument out of range on a List?

Hi! I'm getting an error that I don't understand. Here's the code...

 private List<GameObject> _activeNotifsList;

 ...

 void Start () {
         _activeNotifsList = new List<GameObject>();
 }
 
 ...
 
 public void LerpNotifButtons(){
     //If an active notif exists in slot 0 and it is not yet at its target position, lerp it.
     if(_activeNotifsList[0] != null)
     {
            //Do something
        }
 }

Where the if statement is returning the out of range error. Is it because the list doesn't actually contain any elements yet?

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
1

Answer by Hoeloe · Feb 12, 2014 at 10:22 AM

This means that you have no elements in your list. You're making a simple mistake in your reasoning. The list isn't created infinite in length with all the entries set to null - there are no entries. There is a fundamental difference here. The word null has a very specific meaning, namely that an object has space reserved for it in the heap, but has nothing assigned to it yet. However, the list objects have no memory reserved for them at all. This is fundamentally different. You can refer to an object that exists, and a null check just tests if it has been initialised, but you cannot refer to something that doesn't exist at all.

However, this list has no elements assigned. When you write _activeNotifsList[0], what are you asking it to look up? There is nothing reserved in memory - this object literally does not exist. You can't compare it to anything because there's nothing to compare. I hope you understand this.

So, what can you do instead? Well, you want to check if the list has any elements, so why not just use List.Count to count the number of elements in the list, and check if it is 0 or not? That is the most effective way to do what you seem to want to.

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 Valentin_B · Feb 12, 2014 at 11:22 AM

Where the if statement is returning the out of range error. Is it because the list doesn't actually contain any elements yet?

Yes.

do

 if(_activeNotifsList.Count > 0)
 {
     //do something
 }

instead.

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

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

20 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

Related Questions

A node in a childnode? 1 Answer

Unity Common Errors, List of Common problems and Common Solutions 1 Answer

Null reference exception: Object reference not set to an instance of an object 3 Answers

Cannot cast from source type to destination type. 1 Answer

Need help with ArgumentOutOfRange Error 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