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 jaycode · Jul 16, 2012 at 03:57 AM · c#array

Does C# Support Array of Objects?

I need to do something like following:

public class GameMain : MonoBehaviour {

private Player[] players;

// Use this for initialization void Start () { players[0] = new Player(arenaTexture); players[1] = new Player(arenaTexture); } }

But it returns an error: NullReferenceException: Object reference not set to an instance of an object

Does C# support this? Or is it better if I just use 2 variables: player1 and player2?

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
3
Best Answer

Answer by flamy · Jul 16, 2012 at 04:06 AM

 public class GameMain : MonoBehaviour{
 
 private Player[] players;
 
  void Start()
  {
      players =  new Player[length/*give the number you need*/];
 
      players[0] = new Player(arenaTexture);
 
  }
 
 }


initialization was the problem you had.... in this case player array is fixed... i usually prefer using Lists because of its flexibility.

 using System.Collections.Generic;
 
 public class GameMain : MonoBehaviour{
 
 private List<Player> players;
 
  void Start()
  {
      players =  new List<Player>();
 
      players.Add( new Player(arenaTexture));
 
  }
 
 }
Comment
Add comment · Show 6 · 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 jaycode · Jul 16, 2012 at 04:43 AM 0
Share

Thank you for your answer. By the way, which is more resource-friendly between the two methods above? And by how much?

avatar image flamy · Jul 16, 2012 at 04:47 AM 0
Share

hmm generally array are better for defined number of objects and also it is faster than any other method but list are good if u dont knw the size and also it is efficient and easy way of handling stuffs. Also the difference in execution speed is not much.. It purly depends on ur need.

avatar image Avaista · Jul 16, 2012 at 04:49 AM 1
Share

Its close enough to not really matter. List essentially is an array that reallocates it self as needed. If you know how big you will need it, you can set it. Other wise you can remove from and add to as needed.

Unity can serialize Lists, which means you can edit them in the Inspector.

avatar image Avaista · Jul 16, 2012 at 04:55 AM 0
Share

I should explain: List has a property called Capacity, which it uses to reallocated itself as it expands. Example: If you know that you are going to allocate around 1000 objects, but you are not sure,

 List<int> myList = new List<int>();
 myList.Capacity = 1000;
 //Add Your Items
 myList.TrimExcess();

if it is 999 it will only reallocated the internal array once.

If it is 1100, it will allocate once then start allocating more as you add.

avatar image Kryptos · Jul 16, 2012 at 07:45 AM 1
Share

It is even better to give the inital capacity in the constructor:

 List<int> myList = new List<int>(1000);

This prevents the system from reallocating just after the construction, which can sometimes leads to fragmented memory.

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# - IndexOutOfRangeException - Array index is out of range 2 Answers

Using a footstep C# script and keep getting an "IndexOutOfRangeException" 2 Answers

How to assign List>Vector3>[] 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