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 Jojo B · Feb 13, 2014 at 04:15 PM · c#arraysclasses

Array of classes wont set a length c#

hey so ime making a script that loops through a bunch of items a bunch of times and each item neads the some data stored so i set an array of clases but it seeams like i carnt set its length as when i accesses it it dosent apear to have any value and returns null anyway heres the array and class

public class Water{ public Vector2 curent = new Vector2 (0,0); public float CapedDirt = 0f; } public void Erode (){ Water[] Drops; float BlockErosionAmount = 0.05f; float maxCaped = 0.5f; Drops = new Water[1000]; for (int i = 0; i < MapSize*5; i++){ for (int w = 0; w < 1000; w++){ Vector2 start = new Vector2 (Random.Range(MapSize*0.25f,MapSize*0.75f),Random.Range(MapSize*0.25f,MapSize*0.75f)); print (Drops); print (Drops[w].curent); Drops[w].curent = start;// error coming from here

and hears the error its throwing at me

NullReferenceException: Object reference not set to an instance of an object MapGenBrush.Erode () (at Assets/Standard Assets/MapGenBrush.cs:98) MapGenBrush.CreateHeightMap () (at Assets/Standard Assets/MapGenBrush.cs:81) MapGenBrush.Start () (at Assets/Standard Assets/MapGenBrush.cs:25)

thanks for any help ime asuming this is just my inexperience with c# as ime used to js

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

Answer by Ashish Dwivedi · Feb 13, 2014 at 04:28 PM

Here you are initializing the array but not making the object that's why you are getting error. For creating 100 objects of "Water" class you need to write the following code:

  Drops = new Water[1000];
 for (int i = 0; i < Drops.Length ; i++)
 {
 Drops[i] = new Water();
 }

You can understand better by this Reference. I hope now you would be able to understand.

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 Jojo B · Feb 13, 2014 at 04:41 PM 0
Share

k thanks i get it also am i right in thinking you mean drops.length

avatar image Ashish Dwivedi · Feb 13, 2014 at 04:45 PM 0
Share

Yes, that was a printing mistake. I corrected that.Please remember to accept the answer if it is right for you.

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

20 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

Related Questions

how to construct all arrays (classes) with default constructor (not possible) 2 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Class and Array 1 Answer

Obtaining the Variable of a Prefab in an array. 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