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 steventwerd · Nov 22, 2016 at 01:20 PM · script.sprites2d array

How do I place sprites with my script and a 2D array?

Lets say I create a 5x5 2D array. With a few randomly chosen elements to equal 1.

 int[,] MapArray = new int[4, 4];
 MapArray[0, 3] = 1;
 MapArray[1, 4] = 1;
 MapArray[2, 3] = 1;
 MapArray[5, 1] = 1;

What I want to do is place one of my already made sprites at the position of the 1's in the game so that with enough 1's I could make a good map. (The sprite to go at the 1's can just be a plain square).

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Omar47i · Nov 22, 2016 at 02:12 PM

If i want to place a sprite at the values of 1s i would do the following:

 // defaine a multi-dimentional array an intialize some indices with 1
         int[,] MapArray = new int[5, 5];
         MapArray[0, 3] = 1;
         MapArray[1, 4] = 1;
         MapArray[2, 3] = 1;
         MapArray[4, 1] = 1;
 
         // for each row find the value 1 in each column
         for (int r = 0; r < MapArray.GetLength(0); r++)
             for (int c = 0; c < MapArray.GetLength(1); c++)
             {
                 if (MapArray[r, c] == 1)  // if the value of this index is 1
                 {
                     // .. Instantiate your sprites here 
                 }
             }
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 steventwerd · Nov 22, 2016 at 04:17 PM 0
Share

Yes, this is what I'm looking for but how do I instantiate my sprite (line 14)? Can you explain the code as well please.

avatar image Omar47i steventwerd · Nov 22, 2016 at 05:23 PM 0
Share

I think you should instantiate the sprites as follows:

 Instantiate(yourPrefab, new Vector3(r, c, 0), Quaternion.identity);

avatar image steventwerd Omar47i · Nov 22, 2016 at 05:38 PM 0
Share

Can you also explain what that line of code means? aka new Vector represents the position of where to place the sprite, so what does yourPrefab and Quaternion.identity mean?

Show more comments
avatar image
0

Answer by AidanHorton · Nov 22, 2016 at 02:25 PM

Edit - Update the code to work properly

     //2D Sprite object to spawn in at coordinates
     public GameObject blockObj;
 
     //Map height and mapWidth
     const int mapWidth = 4;
     const int mapHeight = 4;
 
     //Creating the array with width and height of a predefined value
 
     void Start()
     {
         //Create the map, and update the map
         int[,] MapArray = CreateMap();
         UpdateMap(MapArray);
     }
 
     //Define values where the 1's go here
     int[,] CreateMap()
     {
         int[,] mapArray = new int[mapWidth, mapHeight];
 
         //These values must be within [0,0] - [4,4] for a 5x5 grid
         mapArray[0, 1] = 1;
         mapArray[1, 1] = 1;
         mapArray[2, 1] = 1;
         mapArray[3, 1] = 1;
 
         return mapArray;
     }
     
     void UpdateMap(int[,] MapArray)
     {
         //Iterating through the list with 'x' and 'y' representing the x and y cartesian coordinates
         for (int y = 0; y < mapHeight; y++)
         {
             for (int x = 0; x < mapWidth; x++)
             {
                 //If the current index has a value of 1
                 if (MapArray[x, y] == 1)
                 {
                     //Create a block at this point
                     Instantiate(blockObj, new Vector3(x, y, 0), Quaternion.identity);
                 }
             }
         }
     }
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 steventwerd · Nov 22, 2016 at 04:15 PM 0
Share

I copied and pasted it and there appears to be quite a few errors in it that appeared.

avatar image AidanHorton steventwerd · Nov 23, 2016 at 12:55 PM 0
Share

I've imported the script and there were a few issues, but mainly because the code wasn't put into any methods which I assumed you might have done with it :) Anyways, I've got the updated code here. Don't forget to put this code inside your own script in it's own class. Also in the inspector you need to assign the block object you want to spawn in, as the gameobject in the script. I've also update the code in the original post //2D Sprite object to spawn in at coordinates public GameObject blockObj;

     //$$anonymous$$ap height and mapWidth
     const int mapWidth = 4;
     const int mapHeight = 4;
 
     void Start()
     {
         //Create the map, and update the map
         int[,] $$anonymous$$apArray = Create$$anonymous$$ap();
         Update$$anonymous$$ap($$anonymous$$apArray);
     }
 
     //Define values where the 1's go here
     int[,] Create$$anonymous$$ap()
     {
         int[,] mapArray = new int[mapWidth, mapHeight];
 
         //These values must be within [0,0] - [4,4] for a 5x5 grid
         mapArray[0, 3] = 1;
         mapArray[1, 4] = 1;
         mapArray[2, 3] = 1;
         mapArray[4, 1] = 1;
 
         return mapArray;
     }
     
     void Update$$anonymous$$ap(int[,] $$anonymous$$apArray)
     {
         //Iterating through the list with 'x' and 'y' representing the x and y cartesian coordinates
         for (int y = 0; y < mapHeight; y++)
         {
             for (int x = 0; x < mapWidth; x++)
             {
                 //If the current index has a value of 1
                 if ($$anonymous$$apArray[x, y] == 1)
                 {
                     //Create a block at this point
                     Instantiate(blockObj, new Vector3(x, y, 0), 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

6 People are following this question.

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

Related Questions

2D sprite wavering 0 Answers

Get "on screen" color of a texture to use in a legend 2 Answers

How to add sign interaction? 1 Answer

How can I change sprites in animation clip? 0 Answers

Getting last sprite from prefab. 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