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 /
  • Help Room /
avatar image
0
Question by LovePixels · Dec 18, 2015 at 11:22 AM · c#gridcubelists

Make a grid and turn it to List

Hi, i just start programming for unity 3d (3-4 days from now %) ) and make a simple script to make grid from cubes (you can choose how many cells you want to make) and its working... but i think its to complicated what i can change for simplicity? And one more question... how to sort this list? Thanks guyz!

alt text

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CreateAGridFromCube : MonoBehaviour {
 
     public GameObject cube;
 
     public int horCells;
     public int verCells;
 
     public List<GameObject> cellsList = new List<GameObject>();
 
     // Use this for initialization
     void Start () {
         cellsList.Add (cube);
         MakeGrid (horCells,verCells);
     }
 
     void MakeGrid(int hor, int vert){
         for (int x = 0; (x + 1) < hor; x++) {
             GameObject clone = Instantiate (cube);
             clone.transform.position = new Vector3(clone.transform.position.x+1,clone.transform.position.y,clone.transform.position.z);
             clone.name = "1x" + (x + 2);
             for (int y = 0; (y+1) < vert; y++) {
                 GameObject cloneV = Instantiate (cube);
                 cloneV.transform.position = new Vector3(cloneV.transform.position.x,cloneV.transform.position.y-1,cloneV.transform.position.z);
                 cloneV.name = (y + 2)+ "x" + (x + 1);
                 cellsList.Add (cloneV);
                 cube = cloneV;
             }
             cellsList.Add (clone);
             cube = clone;
         }
         int z = cellsList.Count - 1;
         GameObject fuckingLastClones = cellsList [z];
         for (int i = 0; (i+1) < vert; i++) {
             GameObject LastLastClones = Instantiate (fuckingLastClones);
             LastLastClones.transform.position = new Vector3(LastLastClones.transform.position.x,LastLastClones.transform.position.y-1,LastLastClones.transform.position.z);
             LastLastClones.name = (i+2) + "x" + hor;
             cellsList.Add (LastLastClones);
             fuckingLastClones = LastLastClones;
         }
     }
 }
 


capture.png (100.7 kB)
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
1
Best Answer

Answer by OncaLupe · Dec 19, 2015 at 04:29 AM

You definitely made the spawning much more complicated than is needed. For a static length grid I'd also use a 2d array, so that if you need to access a specific grid cube you can just pass in the x and y.

 using UnityEngine;
 using System.Collections;
 
 public class CreateAGridArrayFromCube : MonoBehaviour
 {
     public GameObject cube;
 
     public int horCells = 10;
     public int verCells = 10;
     public Vector3 startPos = new Vector3(0f, 0f, 0f);
     public float spacingX = 1f;
     public float spacingY = 1f;
 
     public GameObject[,] cellsArray;
 
     void Start()
     {
         MakeGrid(horCells, verCells);
     }
 
     void MakeGrid(int hor, int vert)
     {
         cellsArray = new GameObject[hor,vert];
         GameObject clone;
         Vector3 clonePos;
         for(int x = 0; x < hor; ++x)
         {
             for(int y = 0; y < vert; ++y)
             {
                 clonePos = new Vector3(startPos.x + (x * spacingX), startPos.y + (y * spacingY), startPos.z);
                 clone = Instantiate(cube, clonePos, Quaternion.identity) as GameObject;
                 clone.name = (y + 1) + "x" + (x + 1);
                 cellsArray[x,y] = clone;
             }
         }
     }
 }

If you'd rather use the List, then you can replace these lines:

 public GameObject[,] cellsArray;
 cellsArray = new GameObject[hor,vert];
 cellsArray[x,y] = clone;

with these:

 public List<GameObject> cellsList;
 cellsList = new List<GameObject>();
 cellsList.Add(clone);

Then, to access a specific grid cube from the List (remember to start counting from 0):

 cellsList[(x * verCells) + y];
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 LovePixels · Dec 21, 2015 at 02:24 PM 0
Share

Tnx for your help! Try to make this code better :) I like idea with 2D array, try to use it practically!

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

44 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 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 my List adding to same array object again and again in update? 1 Answer

Dynamically Populated Dropdown list has items with the same text 1 Answer

List returns NullReferenceException, but when it's visible in the Inspector it works perfectly ??? 1 Answer

Problem adding List<> objects to another List<> 1 Answer

How to remove items from a target list on a target object's defeat. 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