• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Problem with Singleton and NullReferenceException? 0 Answers

Custom Class Array Serialization Problem 0 Answers

Why Again Arrays In ExecuteInEditMode() Gives NullReferenceException Errors?? 1 Answer

Array of abstract class in inspector 2 Answers

Declare new class[].vector3[] length? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges