Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Fantilyre · Sep 16, 2019 at 03:58 PM · arraynullreferenceexceptionfor-loop

Odd NullReferenceException on FOR loop

For loop throws NullReferenceException, my asumption is because of array index not tolerated BUT I tested index in the same loop to make sure it is valid and it IS valid.

I am puzzled, please help.

 TER_Block nBlock = NewGO.AddComponent<TER_Block>();
 nBlock.Points = ChunkPoints;

 if (ChunkDirections.Count > 1)
                     {
                         for (int i = 0; i < ChunkDirections.Count; i += 2)
                         {
                             Debug.Log("Count = " + ChunkDirections.Count + " / i = " + i + " / " + ChunkDirections[i] + " / " + ChunkDirections[i + 1]);
                             if (isRight) nBlock.Directions.Add(ChunkDirections[i]);
                             else nBlock.Directions.Add(ChunkDirections[i + 1]);
                         }
                     }
     
     // LOG output :
     // ...
     // Count = 4 / i = 0 / (1.0, 0.0, 0.0) /  (-1.0, 0.0, 0.0)
     // NullReferenceException: Object reference not set to an instance of an object [...] at MNG_Terrain.cs : 660
     // Count = 6 / i = 0 / (1.0, 0.0, 0.0) /  (-1.0, 0.0, 0.0)
     // NullReferenceException: Object reference not set to an instance of an object [...] at MNG_Terrain.cs : 660
     // ...

Line 660 of MNG_Terrain is the following bit : else nBlock.Directions.Add(ChunkDirections[i + 1]); .


Values are ALWAYS added by pair to the ChunkDirections list (right and left vector3 s).


As proven by Debug.Log, values for both i = 0 and i = 1 are in ChunkDirections list, but despite this, else statement throws an NRE.


If I delete the ELSE statement, suddenly the error gets thrown on line 659; the line where I use i as an index.


nBlock.Directions absolutely exists, it is used all around the FOR loop without issues, and no variables are static.


please HELP

Comment
Add comment · Show 2
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 Fantilyre · Sep 16, 2019 at 04:23 PM 0
Share

For now I circumvent the issue by creating a new list before the loop and adding values to it through the loop, then copy that list over to the nBlock.Directions list after the loop ended. It works but I am really frustrated at why it doesn't work directly in the loop, I really don't see why. Could someone help please.

avatar image Fantilyre · Sep 17, 2019 at 03:53 AM 0
Share

I forgot to specify that I tested nBlock inside of the loop with other calls (printing its name in a debug log) and it worked fine.

0 Replies

· Add your reply
  • Sort: 

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

200 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 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 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 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 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 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 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 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 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 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

Getting array from Json suddenly doesnt work. 1 Answer

Named array assigned to new array, named array returns null on all subsequent calls 0 Answers

Can't access public static array from another class 1 Answer

[SOLVED] Remove Vector3 Array step by step when GameObject spawned 1 Answer

index out of range problem with for loops and arrays. 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