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 /
This question was closed Jul 15, 2016 at 09:41 AM by joshua-lyness for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by joshua-lyness · Jul 15, 2016 at 12:09 AM · errorarrayclasscustomresize

Error message when accessing class array

I have a class called road, as you can see below :

 public class Road {
         public int typeIndex;
         public int[] intersection;
         public Bezier[] spline; 
     }

I create an array of this class, using :

 Road[] outputRoad = new Road[1];

Within that class, you can see a variable called Bezier[], another array of a custom class. This class is :

 public class Bezier {
         public Vector3[] controlPoint;
         public float length;
         public BoundingBox boundary;
     }

And within that is an array of Vector3[] called controlPoints. Within my code, I need to set controlPoints, however, as expected the code throws up the error

 NullReferenceException: Object reference not set to an instance of an object
 System.Array.Resize[Bezier] (.Bezier[]& array, Int32 newSize) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Array.cs:1913)
 roadTool_cs.BuildRoad (.buildInfo roadInfo) (at Assets/toolManager/roadTool_cs.cs:170)
 roadTool_cs.Update () (at Assets/toolManager/roadTool_cs.cs:84)

Since I haven't allocated memory to the arrays. Now I tried doing this :

 System.Array.Resize (ref outputRoad, 1);
             System.Array.Resize (ref outputRoad[0].spline, 1);
             System.Array.Resize (ref outputRoad[0].spline[0].controlPoint, 4);

To set the size of the arrays, however on the second line, I get the exact same error as the one above, and I have no idea why. I set the size of every array from the road class, the Bezier class, and also the array of Vector3's. What am I doing wrong? I had an idea, to put a method within the class, and write something like:

 outputRoad = new Road[1];

but I STILL get an error. Anyway thanks for your time, hope you can 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 EDevJogos · Jul 15, 2016 at 12:45 AM 0
Share

Your array of of controlPoint have the size 0, but this should throw you a Index out of range, in case you tried to assing something to it. And your Bezier array is also size 0

Your Road array is a nullRef, because in this code you posted, you never actualy put a road in the array, you just allocate a Road array of size 1.

But i can't say much because i'm solo based on what you have posted here, i don't know if you fill this arrays latter, but in the state they are, they won't work.

avatar image joshua-lyness · Jul 15, 2016 at 02:02 AM 0
Share

I create a new Road variable higher up in the script, but at that point in time, the array wouldve been 0 in length. Youre correct, i need to create an instance of it afterwards. Ill see if that works.

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Arkaid · Jul 15, 2016 at 12:46 AM

You're not initializing any array and you can't resize a null. Do this first:

  public class Road {
          public int typeIndex;
          public int[] intersection = new int[1];
          public Bezier[] spline = new Bezier[1];
      }

  public class Bezier {
          public Vector3[] controlPoint = new Vector3[1];
          public float length;
          public BoundingBox boundary;
      }

Then you can probably Resize.

That said, if you're going to be resizing arrays, I'd recommend using List instead.

Comment
Add comment · Show 8 · 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 joshua-lyness · Jul 15, 2016 at 02:03 AM 0
Share

Can i write it in a method within the class? So a method called Public Road () {} and initialise them in there ins$$anonymous$$d?

avatar image Arkaid joshua-lyness · Jul 15, 2016 at 02:14 AM 0
Share

Sure, you could initialize the arrays in the constructor :)

avatar image Bunny83 · Jul 15, 2016 at 09:25 AM 0
Share

Also keep in $$anonymous$$d that this line:

 outputRoad = new Road[1];

only creates a Road array with just one element, but this element is null. You haven't yet created a Road object

 // create Road object and store it as first element in the array.
 outputRoad[0] = new Road();
avatar image joshua-lyness · Jul 15, 2016 at 09:32 AM 0
Share

Well this is my section of code :

 Road[] outputRoad = new Road[1];
             outputRoad[0] = new Road ();
             outputRoad[0].spline [0].controlPoint [0] = roadInfo.userPoint [0];
             outputRoad[0].spline[0].controlPoint[3] = roadInfo.userPoint[2];

And these are my classes :

     public class Bezier {
         public Vector3[] controlPoint = new Vector3[4];
         public float length;
         public BoundingBox boundary;
     }
 
     public class Road {
         public int typeIndex;
         public int[] intersection = new int[0];
         public Bezier[] spline = new Bezier[1];
     }

Yet this still isn't working. Same error,

avatar image joshua-lyness · Jul 15, 2016 at 09:41 AM 0
Share

I added the line : outputRoad[0].spline[0] = new Bezier(); And everything works now. Thing is I've done some similar stuff higher up on my project and I've never needed to do this, write " = new type;" whenever accessing the class within the class. And that's because I initialised it inside the constructor, and forgot to do that with these classes. :)

avatar image joshua-lyness · Jul 15, 2016 at 09:50 AM 0
Share

Why do we have to do this, may I ask? If I create an integer variable, I don't have to write something like

 int randomNum = new integer;

because, if it has value, it's automatically null. And then if I try to set it, it doesn't throw back a random error. Why doesn't unity just automatically initialise the custom class to all default values?

avatar image Arkaid joshua-lyness · Jul 15, 2016 at 10:08 AM 0
Share

That's not a Unity issue, that's C#

You need to understand the difference between a value type and a reference type.

As their name indicate, value types hold a value. int and float are good examples.

Reference type hold not a value but a reference to the actual data. Classes fall in this category.

When you create a reference, you have a variable that can point to where the data is, but until you assign it to something, it points to nothing, that is, you hold a null reference.

Value types on the other hand directly hold a value, so there's no need for a 'new' call

It helps a bit if understand how computer memory works. You should read up on values, references, stack, heap and pointers and you'll understand why the difference :3

avatar image joshua-lyness Arkaid · Jul 16, 2016 at 12:29 AM 0
Share

I know a fair deal, the reference is most likely an address to a memory location and all that stuff xD I didn't know that there were references/values, I understand now. Anyway not it's relevant but I got my program finished and works, thanks for your help. :)

Follow this Question

Answers Answers and Comments

65 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

Related Questions

Custom Class Array Serialization Problem 0 Answers

WARNING : Assignment to temporary. 1 Answer

Add custom declared variables in a Array 1 Answer

C# custom class array error in adding a new entry. 1 Answer

Array of abstract class in inspector 2 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