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 /
  • Help Room /
avatar image
0
Question by pabloferrariferrari · Dec 12, 2019 at 05:55 PM · instantiatelistargumentexceptionargumentoutofrangeexception

Object list is null?

I need to instantiate an object from a list, if it isn't undefined, but the code don't make it. Pseudo-code:

 list<GameObject> list = new List<GameObject>();
 int number;
 
 void Update(){
 if(list[number] != null){
 Instantiate(list[number...);
 }
 }

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 Larry-Dietz · Dec 12, 2019 at 09:23 PM 1
Share

If you are going to instantiate object from the list, first, the list needs to contain something. Your psuedo code doesn't show the list as public, so it wouldn't be populated in the inspector. (Unless your actual code is public for this)

Also, doing this in update, will instantiate an object every frame, probably not what you are wanting here.

You will also want to make sure that number is not outside of the bounds of the array.

avatar image pabloferrariferrari Larry-Dietz · Dec 13, 2019 at 03:16 PM 0
Share

Hi, the list have a method to get objects, and a condition to instantiate, the problem is what you said, the value is outside bounds of list, there is a way to don't instantiate the object if it is outside bounds? (Sorry for my English) The current code:

 using UnityEngine;
 using System.Collections.Generic;
 public class inventory : $$anonymous$$onoBehaviour
 {
     public List<GameObject> items = new List<GameObject>();
     public int slots;
     [HideInInspector] public int number; // O número pressionado - hotbar
 
     private $$anonymous$$eyCode[] keyCodes = { // Array dos números possíveis - hotbar
          $$anonymous$$eyCode.Alpha1, $$anonymous$$eyCode.Alpha2, $$anonymous$$eyCode.Alpha3, 
         $$anonymous$$eyCode.Alpha4, $$anonymous$$eyCode.Alpha5, $$anonymous$$eyCode.Alpha6,   // Definem as "keys" para serem pressionadas 
         $$anonymous$$eyCode.Alpha7, $$anonymous$$eyCode.Alpha8, $$anonymous$$eyCode.Alpha9,
      };
 
     void Update()
     {
         // --- Hotbar
         for (int i = 0; i < keyCodes.Length; i++) // Para cada $$anonymous$$eyCode processa um valor 'i'
         {
             if (Input.Get$$anonymous$$eyDown(keyCodes[i])) // Detecta o valor pressionado (poderia somar um a ele (para compensar o início em 0 das arrays) contudo, seria...
             // ...removido no instantiate)
             {
                 number = i;
                 if (items[number] != null)
                 {
                     Instantiate(items[number], new Vector2(transform.position.x, transform.position.y - 0.5f), Quaternion.identity, this.transform);
                 }
             }
         }
         // ---
     }
 }
 

The comments are in portuguese.

1 Reply

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

Answer by Larry-Dietz · Dec 13, 2019 at 03:25 PM

Make the following change, to ensure that number is within the bounds of the array before trying to instantiate...

   if (number < items.Count && items[number] != null)
   {
       Instantiate(items[number], new Vector2(transform.position.x, transform.position.y - 0.5f), Quaternion.identity, this.transform);
   }

Hope this helps, -Larry

Comment
Add comment · Show 2 · 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 pabloferrariferrari · Dec 13, 2019 at 03:46 PM 0
Share

Thanks! It helped a lot!

avatar image Larry-Dietz pabloferrariferrari · Dec 13, 2019 at 03:48 PM 0
Share

Glad I was able to help.

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

219 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 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 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 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 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 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

Prevent spawning if the gameobject has same position as a list of Vector3s using another list? 1 Answer

Check if a large number of gameobjects are colliding 1 Answer

Unknown Argument Out of Range Index Error On Card Game 1 Answer

Add Clones of GameObject to List(array) 0 Answers

Having trouble with instantiating bullets at multiple points via a list of transforms. 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