• 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
Question by Erik · Sep 12, 2011 at 08:10 AM · editorarraylistgrid

Help with array/lists manipulation in Editor script

Hi,

I'm trying to create an arbitrary 2D grid of cubes in an Editor script, and then select random portions of this grid, just like in this picture:

alt text

I've tried using a 2D array, but it won't work unless I define its dimensions while declaring it (and it's not what I need, because I need the user to set them). Here's the code using arrays:

 using UnityEngine;

using UnityEditor; using System.Collections; using System.Collections.Generic;

public class RNDGEN : EditorWindow {

 private int mapX, mapY = 0;
 
 GameObject[,] mapArray;
 
 GameObject floorTile;
 
 GameObject baseGrid = null;

 
 
 [MenuItem ("Window/My Window")]
 static void Init () 
 {
     // Get existing open window or if none, make a new one:
     RNDGEN rndGen = (RNDGEN)EditorWindow.GetWindow (typeof (RNDGEN));
 }
 

 void OnGUI()
 {
     mapX = EditorGUI.IntSlider(new Rect(3, 3, position.width - 6, 15), "Map width:", mapX, 2, 50);
     
     mapY = EditorGUI.IntSlider(new Rect(3, 25, position.width - 6, 15), "Map height:", mapY, 2, 50);
     
     mapArray = new GameObject[mapX, mapY];
     
     floorTile = (GameObject)Resources.Load("Floor_Tile");
     
     
     if (GUI.Button(new Rect(3, 45, position.width - 6, 20), "Create Grid"))
     {    
         baseGrid = new GameObject();
         
         baseGrid.name = "Base_Grid";
         
         for (int i = 0; i < mapX; i++)
         {    
             for (int j = 0; j < mapY; j++)
             {
                 GameObject tile = (GameObject)Instantiate(floorTile, new Vector3(i, 0, j), Quaternion.identity);
                 
                 tile.transform.parent = baseGrid.transform;
                 
                 tile.name = "Tile_" + i.ToString() + "-" + j.ToString();
                 
                 mapArray[i, j] = tile;
                 
             }
             
         }
     
     }
     
     
     if (GUI.Button(new Rect(3, 75, position.width - 6, 20), "Test"))
     {        
         for (int i = 0; i < Random.Range(0, mapArray.GetLength(0)); i++)
         {
             for (int j = 0; j < mapArray.GetLength(1); j++)
             {
                 if (mapArray[i, j] != null)
                 {
                     mapArray[i, j].renderer.material.color = Color.red;
                     
                 }
                 
             }
             
         }
         
     }

 }

}

Then I tried using nested lists, but it's not working like it should. Maybe I'm doing something wrong while iterating through the nested lists. Here's the code using lists:

 using UnityEngine;

using UnityEditor; using System.Collections; using System.Collections.Generic;

public class RNDGEN : EditorWindow {

 private int mapX, mapY = 0;
 
 List<List<GameObject>> mapArrayX = new List<List<GameObject>>();
 
 List<GameObject> mapArrayY = new List<GameObject>();
 
 GameObject floorTile;
 
 GameObject baseGrid = null;

 
 
 [MenuItem ("Window/My Window")]
 static void Init () 
 {
     // Get existing open window or if none, make a new one:
     RNDGEN rndGen = (RNDGEN)EditorWindow.GetWindow (typeof (RNDGEN));
 }
 

 void OnGUI()
 {
     mapX = EditorGUI.IntSlider(new Rect(3, 3, position.width - 6, 15), "Map width:", mapX, 2, 50);
     
     mapY = EditorGUI.IntSlider(new Rect(3, 25, position.width - 6, 15), "Map height:", mapY, 2, 50);
     
     
     floorTile = (GameObject)Resources.Load("Floor_Tile");
     
     
     if (GUI.Button(new Rect(3, 45, position.width - 6, 20), "Create Grid"))
     {    
         baseGrid = new GameObject();
         
         baseGrid.name = "Base_Grid";
         
         for (int i = 0; i < mapX; i++)
         {    
             for (int j = 0; j < mapY; j++)
             {
                 GameObject tile = (GameObject)Instantiate(floorTile, new Vector3(i, 0, j), Quaternion.identity);
                 
                 tile.transform.parent = baseGrid.transform;
                 
                 tile.name = "Tile_" + i.ToString() + "-" + j.ToString();
                 
                 mapArrayY.Add(tile);
                 
                 mapArrayX.Add(mapArrayY);
                 
             }
             
         }
     
     }
     
     
     if (GUI.Button(new Rect(3, 75, position.width - 6, 20), "Test"))
     {        
         for (int i = 0; i < Random.Range(0, mapArrayX.Count); i++)
         {
             for (int j = 0; j < mapArrayY.Count; j++)
             {
                 if (mapArrayX[i][j] != null)
                 {
                     mapArrayX[i][j].renderer.material.color = Color.red;
                     
                 }
                 
             }
             
         }
         
     }

 }

}

Any help?

Comment

People who like this

0 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 Erik · Sep 12, 2011 at 08:10 AM 0
Share

sorry, the link to the picture is here: http://postimage.org/image/306v7glyc/

1 Reply

  • Sort: 
avatar image

Answer by Owen-Reynolds · Sep 12, 2011 at 07:55 PM

Helpful if we knew what was wrong with the code you have, but... you have this line just sitting in OnGUI, running every frame:

 mapArray = new GameObject[mapX, mapY];

That creates a fresh, empty map (contents are all null) each frame, erasing the old one. I'm thinking you intended to put the mapArray= line inside the "Create New Map" button.

Design-wise, are you wanting to edit a persistant array of cubes? In that case, mapArray should be in one script attached to a gameObject, and the editor should target it. Things declared in the editor are to help you edit, but aren't part of the game. If the cubes are just a visual aid to help you edit something else, than go ahead and leave them declared in the editor script.

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Changes not being saved in the Inspector Editor 1 Answer

How can I assign an Array of Vector3 while seeing and editing the values in the Scene? 0 Answers

How do you emulate array/List Inspector behavior with an Editor window? 0 Answers

Is there a way to remove array entries in the editor? 4 Answers

A node in a childnode? 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