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 Kalgonium · Sep 18, 2012 at 06:53 PM · javascriptarrayclass

How to access a variable within a multidimensional array of class

Hi, I want to create a grid in which each element (position) will have multiple properties. I figured the best way to do it was to crate a class. But as it's going to be a 2D grid, I want to use that class in a 2D variable. Create the variable doesn't ssemto be a problem, but accessing the variables within always gives me errors... Here's the code:

 class carteLayout
 {
     var terrain:int;
     var terrainTemp:int;
     var isroad:boolean;
     var unit:int;
     var blockTerrain:Transform;
 }
 var carte:carteLayout[,];
 var grid:Transform;
 var x:int;
 var z:int;
 
 function Start()
 {
     x = 20;
     z = 20;
     carte = new carteLayout[x,z];
     CreateGrid(x,z);
 }
 
 function CreateGrid(newX:int, newZ:int)
 {
     carte = new carteLayout[newX,newZ];
     for(var i = 0 ; i < newX ; i++)
     {
         for(var l = 0 ; l < newZ ; l++)
         {
             carte[i,l].terrain = 99;
             carte[i,l].isroad = false;
             carte[i,l].unit = 99;
             Instantiate(grid,Vector3(i,0,l),Quaternion.identity);
         }
     }
 }

I always get a NullReferenceException at the line "carte[i,l].terrain = 99"

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by whydoidoit · Sep 18, 2012 at 07:01 PM

When you made the array you just made a place to hold all of the class instances - you actually didn't put anything in there yet. I've added an initialiser to your code shown below:

 carte = new carteLayout[newX,newZ];
 for(var i = 0 ; i < newX ; i++)
 {
    for(var l = 0 ; l < newZ ; l++)
    {
      carte[i,l] = new carteLayout();  //Add this (and carteLayout should be named CarteLayout by convention)
      carte[i,l].terrain = 99;
      carte[i,l].isroad = false;
      carte[i,l].unit = 99;
      Instantiate(grid,Vector3(i,0,l),Quaternion.identity);
    }
 }
Comment
Add comment · Show 4 · 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 Eric5h5 · Sep 18, 2012 at 07:04 PM 2
Share

Also it would be good to make a constructor for the class, which will make setting up each array entry easier.

avatar image Kalgonium · Sep 18, 2012 at 07:16 PM 0
Share

Wonderful ! That did it !

Also, Eric, I'm not familiar with constructors, I tried looking in the unity references, but did'nt find it. Can you tell me where to look ?

Thanks guys !

avatar image Eric5h5 · Sep 18, 2012 at 07:34 PM 0
Share
 class Foo {
     var a : int;
     function Foo (a : int) {
         this.a = a;
     }
 }

So ins$$anonymous$$d of:

 var blah = new Foo();
 blah.a = 42;

You can do:

 var blah = new Foo(42);
avatar image Kalgonium · Sep 18, 2012 at 07:39 PM 0
Share

I can see how it can be a faster way on the long run. I'll definitely try using that. Thanks Eric !

avatar image
2

Answer by Khada · Sep 18, 2012 at 07:08 PM

Changing:

        for(var l = 0 ; l < newZ ; l++)
        {
          carte[i,l].terrain = 99;
          carte[i,l].isroad = false;
          carte[i,l].unit = 99;
          Instantiate(grid,Vector3(i,0,l),Quaternion.identity);
        }

To:

        for(var l = 0 ; l < newZ ; l++)
        {
          carte[i,l] = new carteLayout();
          carte[i,l].terrain = 99;
          carte[i,l].isroad = false;
          carte[i,l].unit = 99;
          Instantiate(grid,Vector3(i,0,l),Quaternion.identity);
        }


should fix your problem.

Also, remove:

          carte = new carteLayout[x,z];


from your start function and also remove:

         x = 20;
         z = 20;


if you want to use the values you define in the unity editor.

EDIT: Sigh, late to the party.

Comment
Add comment · Show 3 · 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 whydoidoit · Sep 18, 2012 at 07:12 PM 1
Share

Good point ono the Start() function - definitely worth the OP doing that...

avatar image Kalgonium · Sep 18, 2012 at 07:15 PM 0
Share

yeah, thanks $$anonymous$$hada ^^

avatar image Khada · Sep 18, 2012 at 07:18 PM 0
Share

You're welcome :)

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

13 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

Related Questions

Reading and Storing External Data into Memory (From Text) 1 Answer

Creating a dynamic array of objects of custom class 1 Answer

Different way to access class variables? 1 Answer

Static array with custom class? 1 Answer

How to increase the size of a class array? 4 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