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 MithosAnnar · Feb 21, 2012 at 01:57 PM · gameobjectarraylistitem

Can't add GameObjects to ArrayList

I am sending information from my item to my inventory. Telling the inventory to add the item. Gives me a null reference exception.

Item.js :

 inventory.AddItem(this as GameObject);    

Inventory.js :

 var items : ArrayList;
 
 function AddItem (item : GameObject) {
          
    items.Add(item.gameObject);  //Error happens here
      
 }
Comment
Add comment · Show 5
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 Jessy · Feb 21, 2012 at 02:02 PM 0
Share

Why are you using an ArrayList if you're only putting one type in it?

avatar image MithosAnnar · Feb 21, 2012 at 02:10 PM 0
Share

the player may want more than 1 item in his/her inventory.

avatar image MithosAnnar · Feb 21, 2012 at 02:43 PM 0
Share

The error is occurring at the line in Inventory.js:

items.Add(item.gameObject);

Still got no idea why.

avatar image Jessy · Feb 21, 2012 at 02:46 PM 0
Share

I'm not asking why you want a collection; I'm asking why you're using an ArrayList. You should only use that if you need to support multiple types.

avatar image MithosAnnar · Feb 21, 2012 at 02:56 PM 0
Share

Ok, im using it because it supports Add().

1 Reply

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

Answer by Jessy · Feb 21, 2012 at 03:01 PM

this is not a GameObject; its class is derived from MonoBehaviour. You use the correct semantics later on:

 .gameObject

Using the shorthand for this.gameObject works fine:

 inventory.AddItem(gameObject);

However, you should use something other than an ArrayList; a List is probably what you want. Using .gameObject on item is useless; I don't even know why that compiles.

 import System.Collections.Generic;
 
 var items : List.<GameObject>;
 
 function AddItem(item:GameObject) {
    items.Add(item);
 }
Comment
Add comment · Show 4 · 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 MithosAnnar · Feb 21, 2012 at 04:26 PM 0
Share

i tried this but it gave me the same error.

avatar image MithosAnnar · Feb 21, 2012 at 04:30 PM 0
Share

i did not read:

import System.Collections.Generic;

you are absolutely correct. Thank you so much for the many and helpful replies!

avatar image Jessy · Feb 21, 2012 at 05:08 PM 0
Share

Sorry, I tried to edit that line to be in the code, as I screwed up the initial answer, but QATO won't take the edit. If you get to the reputation level where you can edit the post, you can see that I actually committed the edit, but it won't display. I am not fond of QATO.

avatar image MithosAnnar · Feb 21, 2012 at 05:10 PM 0
Share

yeah no problem(its a problem I get all the time), thanks all the same.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How To Get A Reference To All Nearby GameObjects? 1 Answer

A node in a childnode? 1 Answer

How to see what element is the current one 2 Answers

Storing complex GameObject structure 1 Answer

Scroll List Problem 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