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 /
avatar image
0
Question by Ozale · Jul 02, 2011 at 08:40 AM · multidimarray

Trouble with 2D Array for board game

G'day community,

I'm working on a Turn-Based Strategy game in Unity, and am up to the 'playing field' itself. I've worked with a (pseudo) multidimensional array of integers before for a maze game, but this project calls for a multidimensional array of gameobjects.

I've got a 4x4 'board' of cubes, each with the same script attached, that changes their colour when the mouse moves over them.

For the 'unit', it has a script that is trying to access a multidimensional array of gameobjects (the 'board' cubes) in another script, using an (int) index for both columns and row, to access the GameObject in the next/previous column or row, and change the colour of it.

Here's what's on the 'unit'. It's supposed to change the colour

 function OnMouseUp() {
 var colIndex = 0;
 var rowIndex = 0;
 
 var targetGameObject : GameObject;
 
 var someScript;
 someScript = GetComponent ("fieldScript");
 
 activeUnit = true;
 
 targetGameObject = someScript.grid[rowIndex][colIndex+1];

}

When I try to run this, the last line returns this error: "Object reference not set to an instance of an object".

Now, in an empty game object I have the below code, which attempts to create a 2d array of gameobjects. Each game object is assigned to the rows using the property inspector.

 var rowA : GameObject[];
 var rowB : GameObject[];
 
 var grid = [
     [rowA],
     [rowB]
 ];

I've worked for hours trying to solve this, but nothing seems to work. I'm posting in the hopes somebody might see the cause of my dilema, but please, I need specifics. I've read almost every post/thread to be found on everything remotely related to what I'm trying to achieve.

Cheers,

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

3 Replies

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

Answer by GuyTidhar · Jul 02, 2011 at 08:59 AM

  1. Is "fieldScript" a script found on the same Game Object as this script is attached to? If not - it should, since you are looking for a component on the same game object.

  2. Is "fieldScript" the exact name of that script? When possible don't look for strings. Instead use script types. If you named your script ""fieldScript", then GetComponent(fieldScript) should work.

  3. Don't call getcomponent and find functions repeatedly. You should only use them for initialization (or drag the game objects to public variables when possible).

in your case:

 var someScript;

 function Start()
 {
   someScript = GetComponent ("fieldScript");
 }
 
 function OnMouseUp() 
 {
   var colIndex = 0;
   var rowIndex = 0;
 
   var targetGameObject : GameObject;
 
   activeUnit = true;
 
   targetGameObject = someScript.grid[rowIndex][colIndex+1];
 
 }

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 Ozale · Jul 02, 2011 at 09:12 AM 0
Share

That script is attached to the 'unit'. The reason I used GetComponent is that to access it otherwise (fieldScript.grid etc.), I needed to make it a static var, which wouldn't work because the two arrays (rows) of gameObjects, once static, wouldn't appear in the property inspector to have the gameObjects inserted into them.

I'd be happy to send you my project file, if you're interested. This one is driving me mad.

avatar image GuyTidhar · Jul 02, 2011 at 09:20 AM 0
Share

You can copy the reference of non static public variables to your static variables and then it should work.

You are more then welcome to post a link to the project file (yousendit.com...) or something and I'll look into it as soon as I can (it'll take a few hours till I can though)

avatar image Ozale · Jul 02, 2011 at 09:55 AM 0
Share

Cheers, it's here - http://www.mediafire.com/?fo1ex2mwt8ui8fg. Creating a static variable to mirror the non-static ones returns an error, 'Array index is out of range'. I'd greatly appreciate if you found time to have a look.

avatar image GuyTidhar · Jul 02, 2011 at 03:08 PM 0
Share

Here are the update scripts - I've changed the unityScript and fieldScript. https://www.yousendit.com/download/$$anonymous$$Fo3$$anonymous$$0d1ZDVC$$anonymous$$TZ4dnc9PQ

avatar image Ozale · Jul 13, 2011 at 02:21 AM 0
Share

Sorry it has been so long. This works! Now that this basic interaction with other gameobjects is working, I can set about creating a better 'board', an proper multidimensional array, preferably. Thanks bloke, this has set me on the right track!

Show more comments
avatar image
1

Answer by Eric5h5 · Jul 02, 2011 at 10:13 AM

It's far better if you just use an actual multidimensional array:

 var board : GameObject[,];

 function Start () {
     board = new GameObject[4, 4];
 }
Comment
Add comment · Show 5 · 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 Ozale · Jul 02, 2011 at 12:08 PM 0
Share

How would I populate that array then? It's important which order the GameObjects appear.

avatar image SilverTabby · Jul 02, 2011 at 05:18 PM 0
Share

for(var X : int = 0; X < board.GetLength(0); X++)

for(var Y : int = 0; Y < board.GetLength(1); Y++) {/* board[X,Y].doSomething(); */}

avatar image Eric5h5 · Jul 02, 2011 at 05:38 PM 0
Share

@Ozale: probably the easiest way would be to instantiate the gameobjects in the array from a prefab.

avatar image Ozale · Jul 04, 2011 at 12:01 AM 0
Share

@Eric5h5: do you know of any resources/tutorials for this technique?

avatar image Eric5h5 · Jul 04, 2011 at 01:32 AM 0
Share

@Ozale: Use SilverTabby's code, plus

 board[x,y] = Instantiate(prefab);

If you mean tutorials for instantiating, the docs cover it pretty thoroughly.

avatar image
0

Answer by Waz · Jul 02, 2011 at 09:34 AM

This will not work:

 var rowA : GameObject[];
 var rowB : GameObject[];
 
 var grid = [
     [rowA],
     [rowB]
 ];

At the time grid is initialised, rowA and rowB are null. The only way I see this working is to set grid in Awake().

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Passing a Multidimensional array in Javascript 3 Answers

multidimensional builtin vector3 array? 3 Answers

Visializing a multidimensional array 3 Answers

Arrays vs Coordinates 2 Answers

Multidimensional .net array problem 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