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 scum · Sep 23, 2014 at 10:01 PM · javascriptarrayoop

Return reference to object added to an array (Javascript)

I have a very simple issue. I'm adding an object and trying to return a reference to that object using the following code...

 public var pages: Page[];
 
 public function AddPage (obj : Page) {
    pages += [obj];
    return pages[pages.Length-1]; //return handle to object
 }

Now I call this like so

 var myTestPage = myAdventure.AddPage( new Page("testPage") );

and get this error

Object reference not set to the instance of an object.

Now there doesn't seem to be any issue with the construction of the object as calling AddPage without assigning to an object works fine and can be referenced perfectly.

Many thanks in advance!

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

1 Reply

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

Answer by Eric5h5 · Sep 23, 2014 at 10:08 PM

Arrays are fixed-length and can't have items added to them. Use a generic List instead.

Comment
Add comment · Show 7 · 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 scum · Sep 23, 2014 at 11:18 PM 0
Share

Fixed-length? Does that mean once I have declared an array I cannot add or remove items from it?

avatar image scum · Sep 23, 2014 at 11:23 PM 0
Share

Ah O$$anonymous$$ found some good reference material here -> http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F

Thanks for the help!

avatar image scum · Sep 23, 2014 at 11:41 PM 0
Share

Edit. It Works! Thanks (need to declare my list using constructor)

 public var pages : List.<Page> = new List.<Page>();    
 
 public function AddPage (obj : Page) {
     pages.Add(obj);
     Debug.Log(pages.Count);
     return pages[pages.Count-1]; //return handle to object
 }
avatar image Eric5h5 · Sep 24, 2014 at 12:00 AM 0
Share

Just so you know, the function is returning the exact same object that you're adding, so there doesn't seem to be much point to having the function return anything.

avatar image scum · Sep 24, 2014 at 12:02 AM 0
Share

O$$anonymous$$ One more question in regards to this

return pages[pages.Count-1]; isn't working, its not returning the object.

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Sorting arrays? 1 Answer

How to delete new'd arrays? 2 Answers

what is the difference between a float[] and a float[,]? 1 Answer

Parse Array data to generate html 0 Answers

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 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