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 kaiyum · Oct 28, 2013 at 08:58 PM · arraysinitializationnull exception

vector2D Array initialization not working.

I need to store a bunch of 2d mouse/touch position. So I created an array container which will store these positions. How many positions will be stored, is not fixed;it depends on another public variable which will be manipulated by user at editor. The code looks like this:

     public int points=10;
     private Vector2[] positionBox;

I get warning, that I should initialize my array variable. If I initialize array with one member, array got its size fixed(which I do not want). If I do not initialize it, warning pops up. And in play-mode I get a nice "nullException".

What is the proper way of declaring varying sized array? or I can not declare an array of varying size?

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

3 Replies

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

Answer by jonnyhopper · Oct 28, 2013 at 09:03 PM

You want to use a List of Vector2 instead. To do this you add

using System.Collections.Generic;

at the top of your file and then declare

List< Vector2 > positionBox = new List< Vector2 >()

then you can call positionBox.Add() and positionBox.Remove() etc

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
2

Answer by robertbu · Oct 28, 2013 at 09:08 PM

The declaration for 'positionBox' is not an array. It is a variable that can be pointed to some memory that can be used as an array. If you know at edit time the size of the array, you can put this in Awake() or Start():

 positionBox = new Vector2[points];

This will allocate the memory for the array and set 'positionBox' as a reference to the array. Another possibility is to make 'positionBox' public. This would allow someone to set both the size of the array and the array entries to specific values.

If you are going to be doing actions at runtime that change the size of the array...adding additional elements, removing elements and the like...then you want to use a different kind of collection. The typical one used is generic Lists. You can read about the many different collection types available here:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F

Comment
Add comment · Show 3 · 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 Tomer-Barkan · Oct 28, 2013 at 09:26 PM 0
Share

This is the best way to go if the number of points is known in advance and does not change after the game starts.

Another way to go is make the array public ins$$anonymous$$d of private, which will allow you to set its size directly in the inspector, without adding a points variable at all.

 public Vector2[] positionBox = new Vector2[10];
avatar image kaiyum · Oct 29, 2013 at 08:19 AM 0
Share

definitely helpful, thanks

avatar image Tomer-Barkan · Oct 29, 2013 at 08:27 AM 0
Share

please accept his answer if it solves your problem. Thanks

avatar image
1

Answer by VesuvianPrime · Oct 28, 2013 at 09:05 PM

If the array is going to change size, your best bet is to use Lists.

 using System.Collections.Generic;
 
 private List<Vector2> positionBox = new List<Vector2>();
 
 positionBox.Add(new Vector2(0, 0));

Dot Net Pearls is an excellent resource for learning C#:

http://www.dotnetperls.com/list

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

18 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

Related Questions

Initialization of Classes Instantiated in an Array in the Inspector 1 Answer

How do i make sure an Object is initialized before using it ? C# 2 Answers

How to use arrays with custom inspector code? 1 Answer

C# array initialization 3 Answers

Array not Initialized for some reason 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