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 Wiplash · Aug 21, 2011 at 03:41 PM · arraygameobjectstilesisometric2.5d

Creating a 2d array of gameObjects

Hi there,

I know this question has been asked a few time before but I can't find an efficient solution to my problem. I want to create a 2.5D isometric game in Unity. I need to create a grid of gameObjects that will be my level.

At the moment I have this code.

var blockPiece1:GameObject; var blockPiece2:GameObject;

var levelArray:Array = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

function Awake() {

 for(x=0; x<20; x++){
     for(z=0; z<20; z++){
     print("x: " + x + " z: " + z + " type: " + levelArray[z*20 + x]);
     if(levelArray[z*20 + x] == 1){
          newPiece = Instantiate (blockPiece1, Vector3(x,1,z), Quaternion.identity);
     } else {
         newPiece2 = Instantiate (blockPiece2, Vector3(x,1,z), Quaternion.identity);
         }
     }
 }

}

This works but seems to be a very inefficient way of creating the Array. I need to be able to click objects and change their type within some kind of Array.

Comment
Add comment · Show 1
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 · Aug 21, 2011 at 04:20 PM 1
Share

There's rarely a reason to use Array; use int[] ins$$anonymous$$d, then your code will be much faster and better.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Aug 21, 2011 at 04:13 PM

You can create a bidimensional GameObject array and fill it when creating your pieces. Later, at Update, you can access each object using this array - something like obj = pieces[row,col];:
EDITED: Included @Eric5h5 suggestion - changed levelArray to an int array - it's much more efficient than the Array class, which is untyped and requires a lot of system resources (memory and CPU time). It makes negligible difference in this script, but may serve as an useful example in other cases.

var blockPiece1:GameObject; var blockPiece2:GameObject; var levelArray:int[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0, 0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, 0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; var width = 20; var length = 20; var pieces: GameObject[ , ];

function Awake() { pieces = new GameObject[width, length]; // creates the object array for(x=0; x<width; x++){ for(z=0; z<length; z++){ print("x: " + x + " z: " + z + " type: " + levelArray[z*20 + x]); var prefab: GameObject; if(levelArray[z*20 + x] == 1) prefab = blockPiece1; else prefab = blockPiece2; pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity); // you can also add a significant name to each piece: pieces[x,z].name = "Piece"+x.ToString("D2")+z.ToString("D2"); } } }

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 Wiplash · Aug 21, 2011 at 05:53 PM 0
Share

Hey,

Thanks for that. Is there a better way to deter$$anonymous$$e what block gets placed. I will have a giant if statement with all my block types at the moment.

Thanks

Will

avatar image Doddler · Sep 02, 2011 at 10:11 PM 0
Share

I think what you want is an array of prefabs. That way you can simply use the level array number as a lookup into your prefab array. In c# I know it's relatively easy to make a public array of prefabs, but I'm not certain on how to do it in Java.

avatar image Eric5h5 · Sep 02, 2011 at 10:20 PM 0
Share

@Doddler: Unity doesn't use Java. In Javascript, it's basically the same.

avatar image watchoutfree · Oct 11, 2012 at 04:39 AM 0
Share

I did that, but it tells me that transform[,] cannot be passed to transform. It's like the instantiate object is'nt a transform[,]

avatar image
0

Answer by NE07HEKIN6 · Sep 02, 2011 at 07:35 PM

Sorry, i was trying to use this script, but im not able to reformulate this to add more prefabs, i try a few way but i can´t figure it the right one. If anyone knows how i will apreciate a lot your help.

Comment
Add comment · Show 2 · 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 aldonaletto · Sep 02, 2011 at 08:24 PM 0
Share

If you have several prefabs, it's better to create a GameObject array and fill it in the Inspector, then select the desired one with a switch statement, like this:

var blockPiece: GameObject[]; // fill this array in the Inspector

... var prefab: GameObject; switch (levelArray[z*20 + x]){ case 1: prefab = blockPiece[0]; break; case 2: prefab = blockPiece[1]; break; case 3: prefab = blockPiece[2]; break; case 4: prefab = blockPiece[3]; break; } pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity); ... But there's an even easier way: ins$$anonymous$$d of the switch statement, use levelArray[] directly as the blockPiece index:

...
        var prefab: GameObject;
        prefab = blockPiece[levelArray[z*20 + x]];
        pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity);
...
avatar image NE07HEKIN6 · Sep 02, 2011 at 09:09 PM 0
Share

Thats it!! Thats what i was looking for! Great man, thanks for your help.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking if a position is occupied in 2D? 1 Answer

I'm attempting to make an array of Gameobjects with javascript but when I want to transform an object in the array, I get an error 1 Answer

How to get adjcent tiles (horizontal or vertical) in an isometric tilemap ? 1 Answer

Having trouble iterating through an array of GameObjects of a certain type 1 Answer

How do you get the tile on which a gameObject is standing on Isometric Tilemap ? 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