• 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 Chris D · Apr 17, 2011 at 09:30 PM · 2darray

Trouble populating 2D array for Match-3 Game

Hi All,

I'm trying to teach myself the mechanics behind a match-3 game and am having some issues handling my arrays. The code below seems to create my 2D array (at least, it gets through the CreateBoard function...) but when I go to try to populate it, things fall apart:

var numSymbols:int = 4; var symbols:Array = new Array(numSymbols); var currentBoard:Array;

 //the symbols being used atm.
 symbols[0] = "triangle";
 symbols[1] = "circle";
 symbols[2] = "square";
 symbols[3] = "ex";

 public function CreateBoard(x:int, y:int):Array {
     //create an array with of x elements. 
         //Each element will be an array of y elements.
     var gameBoard = new Array (x);
     for (var i in gameBoard)
         gameBoard = new Array(y);
     print("created");
     return gameBoard;
 }

 // 'a' is never getting a proper variable passed to it, it seems. 
 public function PopulateBoard(a:Array):Array{
     var toPop:Array = a;
     for (var i in toPop){
         for (var j in toPop[i])
             toPop[i] = symbols[Random.Range(0,numSymbols)];
     }
     print("populated");
     return toPop;
 }

 //I think I broke it. Just fix this, then start testing the output.
 function Update(){
     if (Input.GetButton("Fire1")){
         currentBoard = CreateBoard(5,4);
         currentBoard = PopulateBoard(currentBoard);
     //for (var i in currentBoard) print(currentBoard);
     }
 }

The specific error I'm getting is

NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible)
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value)
Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value)
gameLogic.PopulateBoard (UnityScript.Lang.Array a) (at Assets/gameLogic.js:24)
gameLogic.Update () (at Assets/gameLogic.js:35)

To me it sounds like the 'a' in PopulateBoard isn't getting passed the proper information but, as far as I can tell, it should be...

I decided to use JS arrays as, from what I understand, unity's built-in ones don't have as much flexibility. Also, I've seen the question here: http://answers.unity3d.com/questions/16431/passing-a-multidimensional-array-in-javascript but I'm trying to understand why my approach wouldn't work :(

Can someone show me the error of my ways?

Thanks in advance and sorry for the massive wall-o-code

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 Eric5h5 · Apr 17, 2011 at 10:05 PM

There's no reason to use Array; it's slow and not typesafe. Just use a 2D array (the answer you reference is outdated). Also you should use ints rather than strings, since it's faster and simpler. You can use enums if you want human-readable values instead of magic numbers.

enum Symbols {Triangle, Circle, Square, Ex}
var currentBoard : int[,];

(As to the actual problem, you're assigning gameBoard over again for every iteration through the array, and the "i" in "for (var i in gameBoard)" is never used.)

Comment
Add comment · Show 1 · 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 Chris D · Apr 17, 2011 at 10:59 PM 0
Share

oh wow, ok. I really was making things harder than they needed to be. Thanks a bunch!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

2D Array of GameObjects... 1 Answer

How to remove null's from 2d array ? It's removing but only 2 out of 4 null's. 0 Answers

How to Instantiate Objects at specific Spawn Points without overlapping? 0 Answers

Call Methods from inactive GameObjects 1 Answer

Array based floor 3D game flat plane 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