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 galaboy · Sep 05, 2014 at 12:33 PM · aiarraysscriptingbasics

how to instantiate gameobjects in a 2d array.

am doing a match the card type game..i need help in instantiating game objects in a grid like pattern at run time using arrays.am trying to learn how to do it,but a little help on how to do it will be helpful. thanks in advance. for example like this. alt text

match pair.jpg (15.4 kB)
Comment
Add comment · Show 2
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 Exbow · Sep 05, 2014 at 12:48 PM 0
Share

People in here put time to help each other from their own free/working time. So ins$$anonymous$$d of asking for code freely, you better try something out for yourself and if you stack anywhere paste your code to get help. People can put time in showing you what is wrong with your code or even help you reform it. But surely not to just hand you scripts from scratch when you put zero effort on your own.

avatar image galaboy · Sep 06, 2014 at 05:48 AM 0
Share

boss i never asked for code, i only asked only for suggestionson how to accomplish it. since i have never tried it before, i needed some help on the concept. please read the question once again, i asked help on how to do and not the code.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Fohristiwhirl · Sep 05, 2014 at 02:06 PM

You don't actually need a 2D array really. You could have a 1D array of game objects. Then the width and height of the grid grid to access it.

 public class GridBuilder : MonoBehaviour {
 
     public int gridWidth = 5;
     public int gridHeight = 5;
     public GameObject[] cells;
     public GameObject prefab;
 
     void Start () 
     {
         cells = new GameObject[gridWidth * gridHeight];
 
         for (int y = 0; y < gridHeight; y++)
         {
             for (int x = 0; x < gridWidth; x++)
             {
                 int index = x + y * gridWidth;
                 cells[index] = prefab;
 
                 GameObject.Instantiate(cells[index], new Vector3(x,y,0), Quaternion.identity);
 
             }
         }
 
     }
 }
 

This solutions ignores important things you may need to consider such as spacing between cells. Using more than one prefab to create pairs. Then shuffle the array to create randomness and a different game each time.

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 galaboy · Sep 06, 2014 at 05:53 AM 0
Share

thanks for the help, on the concept.

avatar image
2

Answer by OrbitGames · Sep 05, 2014 at 01:20 PM

untested piece of code

     //create GO prefab
     //create 2d GameObject array named 2dArray
 
     for (int x = 0; x < 4; x++)
        {
           for (int y = 0; y < 4; y++)
              {
              2Darray[x,y] = (GameObject) Instaniate(prefab,new Vector3(x,0,y),Quaternion.Identity);
              }
        }
 


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 galaboy · Sep 06, 2014 at 05:52 AM 0
Share

thanks for the help. this piece of code gave me the idea on how to work on. thanks.

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

25 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 avatar image 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

Why is everything being set the same? Please Help! 1 Answer

Can someone reshape my script so that it is more clean and tidy? it works just looks ugly 3 Answers

Put GameObject array into Transform Array? 1 Answer

need help in the instantiation script 1 Answer

makeing enemies 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