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 Common_Horse · Jun 02, 2012 at 08:20 AM · arraynullreferenceexceptionclasscustomnew

"Null Reference Error" when using a custom class as an array

Hey everyone!

I have been using a code that has a custom class in it and have come across a problem.

When this code is called at runtime:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class CustomClass {
     public Transform aTransform;
     public string aName;
 }
 
 [System.Serializable]
 public class Test : MonoBehaviour {
 
     public CustomClass[] cClass;
     
     void Start () {
         cClass = new CustomClass[1];
         cClass[0].aName = "A new name";
     }
 }

I get this null reference error:

NullReferenceException: Object reference not set to an instance of an object

I fairly sure the problem is when I change the value of cClass to be of length 1, but I do not know how to fix the problem.

Can anyone give me a hand? Cheers.

-AJ

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

Answer by whydoidoit · Jun 02, 2012 at 08:52 AM

You need to create an instance of the custom class as well as the array (it's different with structs).

As well as the array creation you need:

   cClass[0] = new CustomClass();
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 Common_Horse · Jun 02, 2012 at 08:58 AM 0
Share

Thank you very much!

Is there any way to avoid having to do this?

Thanks again,

-AJ

avatar image Eric5h5 · Jun 02, 2012 at 09:00 AM 0
Share

No, that's a basic aspect of how classes work; they are null unless initialized. I'd suggest adding a constructor though.

avatar image Common_Horse · Jun 02, 2012 at 09:09 AM 0
Share

Cool, good to know. Will be doing!

Thanks for your help!

-AJ

avatar image Fattie · Jun 02, 2012 at 09:53 AM 0
Share

The only way to avoid doing it, is to teleport to the year 3755AD, where computers don't have memory, but work with neo-quantum disentangled reversible hyperstates. But don't tell anyone I told you that.

avatar image Common_Horse · Jun 02, 2012 at 10:02 AM 0
Share

At first is was like:

Ohh! That could be cool!

But then I was like

Ohh! That would be freaking amazing!

GIIIIIIII$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$$$anonymous$$IIIIIIEEEEEE!

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

6 People are following this question.

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

Related Questions

transform in a custom class 2 Answers

Pushing GameObject into JS Array returns NullReferenceException 1 Answer

Instantiating gameobjects in an array / class problem | NullReferenceException: 0 Answers

Problem with Singleton and NullReferenceException? 0 Answers

Custom Class Array Serialization Problem 0 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