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 /
  • Help Room /
avatar image
0
Question by iKabyLake30 · Dec 22, 2020 at 12:12 PM · arraynull reference exceptioncustom classclass object

Issues with setting values in custom class

Heyo .. I'm trying to save all object currently in scene. In my concept, I can Instantiate random objects from prefabs array with random rotation. I wanna save objects as custom class:

 [Serializable]
 public class LevelData
 {
     public string levelName;
 
     public BlockData[] blockData;
 }
 
 [Serializable]
 public class BlockData
 {
     public int blockId;
     public string blockName;
 
     public Vector3 blockPos;
     public Vector3 blockRot;
 }



First class is the one I'm saving - there's levelName and array of all blocks called blockData - that's another custom class with blockId (array index from prefabs array) and vector3 for position and rotation.

I've already created object of LevelData class as follows:

 public LevelData levelData;



Then when "block gathering" is triggered ... array foundBlocks is populated with all the blocks with given tag and levelData.blockData is set to be as big as foundBlocks array is. Then I'm setting these blocks into levelData.blockData array:

 for(int i = 0; i < foundBlocks.Length; i++)
         {
             Debug.Log("Writing " + i + " element");
 
             levelData.blockData[i].blockId = blockNameToArray[foundBlocks[i].name];
             levelData.blockData[i].blockName = foundBlocks[i].name;
 
             levelData.blockData[i].blockPos = foundBlocks[i].transform.position;
             levelData.blockData[i].blockRot = foundBlocks[i].transform.eulerAngles;
 
             Debug.Log(i + " done");
         }



And there's a problem: when setting blockData[i].blockId, it throws NullReferenceException:

 NullReferenceException: Object reference not set to an instance of an object
 LevelEditor.FindLevelBlocks () (at Assets/LevelEditor.cs:90)
 LevelEditor+<SaveToFileEn>d__9.MoveNext () (at Assets/LevelEditor.cs:135)
 UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <3dc54541a2574ac7826a004a212a4332>:0)
 UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
 LevelEditor:SaveToFile(Boolean) (at Assets/LevelEditor.cs:119)
 UnityEngine.EventSystems.EventSystem:Update() (at C:/Unity/Editor/2019.4.0f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)

So first I thought it could be problem was usage of dictionary (just to "convert" block's name to prefab array index), but it still throws this exception even with fixed ID value. I'm not 100% sure, but inicialization of blockData array could not be a problem, coz it's just fine in the inspector even with right number of elements.


Must be mentioned that it sometimes worked - randomly, now it's not working at all .. So could someone point me in right direction?

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

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

169 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

Related Questions

How to modify a 2d array [,] of a class? 1 Answer

Save a custom Class Array 0 Answers

Unity custom class array serialization issue. 0 Answers

Null Reference exception with custom class object 0 Answers

How can i fix the null reference? 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