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 Pearbook · Apr 01, 2013 at 01:03 PM · arraysleveltileslevel-editorlevel-map

Having some issues with a Level Array?

Hello,

so I have this level array script but I just could figure out how to add more tiles. When I got it to work it placed every tiles twice as much. I know it has something to with the else statement but I just can't figure out what it is, because when I only copy the if and not the else with it, it still makes twice as much tiles. Already thanks for your help

 var blockPiece1 : GameObject;
 var blockPiece2 : GameObject;
 
 var levelArray:int[] = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
                         1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
                         1,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,0,0,0,1,
                         1,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,1,0,0,0,0,0,0,0,0,1,
                         1,0,0,0,0,0,1,1,1,0,1,1,1,1,0,0,0,0,0,1,
                         1,0,0,0,0,1,1,1,1,0,1,1,1,1,1,0,0,0,0,1,
                         1,0,0,0,1,1,1,1,1,0,1,1,0,1,1,1,0,0,0,1,
                         1,0,0,0,1,1,0,0,0,0,1,1,1,1,1,1,0,0,0,1,
                         1,0,0,0,1,1,1,1,1,0,0,1,1,0,1,1,0,0,0,1,
                         1,0,0,0,0,1,1,1,1,1,0,1,1,1,1,0,0,0,0,1,
                         1,0,0,0,0,1,1,1,1,1,0,1,1,1,0,0,0,0,0,1,
                         1,0,0,0,0,0,1,1,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,1,0,0,0,1,
                         1,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,0,0,0,1,
                         1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
                         1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
                         1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1];
 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);
                 pieces[x,z].name = "tiles";
 
         }
     }
 }

 
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
0
Best Answer

Answer by robertbu · Apr 01, 2013 at 01:57 PM

You have declared a a 1D array, but are indexing it as a 2D array using this logic:

 z*20 + x

'20' is the length (height) of the 2D array. A better programming practice would be to set it to the length:

   if(levelArray[z*length + x] == 1)

After making this change, you need to add whole rows or columns to the data and make the appropriate change to the 'width' and 'length' variables. Since they are public, you need to make the change in the Inspector, not in the code.

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 Pearbook · Apr 01, 2013 at 02:21 PM 0
Share

This does fix the resizing of the level, thanks. But I actually was looking for a way to add more GameObject to the array. At this moment I am using a the 0 as floor and the 1 as collider (tree) but I would like to add a 2 ect, etc but I can't get it to work.

avatar image Next Beat Games · Apr 01, 2013 at 02:48 PM 0
Share

what about using:

 var tileType = levelArray[z*20 + x];

 if (tileType == 1)
     prefab = blockPiece1;
 else if (tileType == 2)
     prefab = blockPiece2;
 else if (tileType == 3)
     prefab = blockPiece4;
 else if (tileType == 4)
     prefab = blockPiece4;
avatar image Pearbook · Apr 01, 2013 at 02:57 PM 0
Share

without

 pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity);

the blockPieces won't be placed, but whenever I try to add it, else if won't work

avatar image Ubahs · Apr 01, 2013 at 03:09 PM 0
Share

Can you explain why adding else if won't work?

I don't believe Next Beat Games was saying to get rid of pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity);

but, if you put it after this if/else if/etc block it seems like it should work, since all that line cares about is prefab.

avatar image Pearbook · Apr 01, 2013 at 03:37 PM 0
Share

It's working, at the end of all the else if, add the else with the Instantiate. Thank you so much

 if (tileType == 1) //Tile 1
                 prefab = blockPiece1;
             else if (tileType == 2) // Tile 2
                 prefab = blockPiece3;
             else
                 prefab = blockPiece2; //Floor
                 pieces[x,z] = Instantiate (prefab, Vector3(x,1,z), Quaternion.identity);

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

12 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

Related Questions

Accessing/Initializing array that's part of a class. 1 Answer

how to access objects in another scene but in the same project? 2 Answers

Level alignment script crashing after a few iterations 2 Answers

InvalidCastException: Cannot cast from source type to destination type - can't seem to instantiate objects into an array! 0 Answers

2D Tilemap Extras: Is there any way to convert a RuleTile into a normal tilemap, so that I can edit it manually? 0 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