Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Dwyaa41 · Jul 14, 2015 at 06:39 AM · c#tilemap2d array

2D Array Tilemap Help [C#]

I'm trying to set up a basic 2D array to use a tilemap where the tile used is based on the integer in the array. I'm having trouble with which tile is being switched (it always seems to be up up one vertically).

2D array creation:

     public int xMax = 3;
     public int yMax = 3;
     
     void Start () {
         int[,] dungeonMap = new int[xMax,yMax];
         PopulateDungeon(ref dungeonMap);
         CreateDungeon(dungeonMap);
     }

Filling dungeon with blank tiles and a test tile at (0,0):

     public void PopulateDungeon(ref int[,] dungeonMap){
         for(int x = 0; x < dungeonMap.GetLength(0); x++){
             for(int y = 0; y < dungeonMap.GetLength(1); y++){
                 dungeonMap[x,y] = 1;
             }
         }
         dungeonMap[0,0] = 2;
     }

Instantiating/re-naming the tiles to the world:

     public void CreateDungeon(int[,] dungeonMap){
         Vector2 tileLoc;
         Sprite spr;
         SpriteRenderer sr;
         GameObject test;
         for(int x = 0; x < dungeonMap.GetLength(0); x++){
             for(int y = 0; y < dungeonMap.GetLength(1); y++){
                 tileLoc = new Vector2((x*.16f), (y*.16f));
                 test = Instantiate(blankTile, tileLoc, Quaternion.identity) as GameObject;
                 test.name = (x + "," + y);
                 test.transform.parent = gameObject.transform;
                 spr = GetTile(dungeonMap[x,y]);
                 sr = blankTile.GetComponent<SpriteRenderer>();
                 sr.sprite = spr;
             }
         }
     }

The output looks like this:
alt text
I can't for the life of me figure out why it is in the 0,1 instead of 0,0 position. Any help would be greatly appreciated!

unityquestion.png (9.3 kB)
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 Cherno · Jul 14, 2015 at 06:59 AM 0
Share

Insert a Debug.Log line to check the tileLoc position. If it is 0,0, then maybe your sprite has it's pivot offset?

1 Reply

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

Answer by barbe63 · Jul 14, 2015 at 10:34 AM

Ok I think I got it!

You are calling a GetComponent on the Prefab instead of the instantiated GameObject, so it looks like you are setting the sprite for the next gameobject... which is 0,1 for 0,0!

Do like this instead:

 sr = test.GetComponent<SpriteRenderer>();
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 Dwyaa41 · Jul 15, 2015 at 01:58 AM 0
Share

This worked thanks a lot!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

2D Jagged Array , Array index is out of range. 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Values in Array Automatically Revert 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