Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Alex_terrarian · Jan 07, 2021 at 02:20 AM · instantiatevariableslistsgrid based game

How do you store instantiated tiles using a coordinate system?

Hi community. I'm trying to produce a program that generates a gridmap, where each instantiated tile has a script attached generating data unique to each tile. I'm struggling to understand how I can define the name of these instantiated tiles using a coordinate system, shown in the code below. I was attempting to use lists with a row and column value, though it appears lists cannot be used as variable names.


The code (The segment between the two for loops is place where this needs to happen):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GridGeneration : MonoBehaviour
 {
     public int rows = 10;
     public int cols = 10;
     public float tileSize = 5f;
         
     void Start()
     {
         generateGrid();
     }
 
     void generateGrid()
     {
         GameObject referenceTile = (GameObject)Instantiate(Resources.Load("blankTile"));
         for (int row = 0; row < rows; row++)
         {
             for (int col = 0; col < cols; col++)
             {
                 List<int> tileCoordinates = new List<int> { row, col };
                 GameObject tile = (GameObject)Instantiate(referenceTile, transform);
                 tile.AddComponent<tileData>();
                 Debug.Log(tile);
                 float posX = col * tileSize;
                 float posY = row * tileSize;
             }
         }
         Destroy(referenceTile);
 
         float gridWidth = cols * tileSize;
         float gridHeight = rows * tileSize;
         transform.position = new Vector2((gridWidth - tileSize) / -2, (gridHeight - tileSize) / 2);
     }
 }
 

Thanks for your time!

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
1
Best Answer

Answer by SuperCats14 · Jan 07, 2021 at 03:48 AM

Hi. You can use GameObject[,] to store the gameobject in a coordinate-like way.

 GameObject[,] tiles;
 int maxX;
 int maxY;
     
 void Start()
 {
     tiles = new GameObject[maxX, maxY];
 }
     
 for (int x = 0; x < maxX; x++)
 {
     for(int y = 0; y < maxY; y++)
    {
        GameObejct tileObject = Instantiate(tile, somePositionIDK, someRotationIDK);
        tiles[x, y] = tileObject;
    }
 }
Comment
Add comment · Show 6 · 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 Alex_terrarian · Jan 07, 2021 at 04:08 AM 0
Share

Hi @SuperCats14 . I'm getting a null reference exception error on my equivalent of the "tiles[x, y]" line.


The tile variable game object declaration:

 public GameObject[,] tile;

The assignment of the game object within the loop:

 GameObject referenceTile = (GameObject)Instantiate(Resources.Load("blankTile"));
         for (int row = 0; row < rows; row++)
         {
             for (int col = 0; col < cols; col++)
             {
                 GameObject rawTile = (GameObject)Instantiate(referenceTile, transform);
                 rawTile.AddComponent<tileData>();
                 tile[col, row] = rawTile;
                 Debug.Log(tile);
                 float posX = col * tileSize;
                 float posY = row * tileSize;
             }
         }

Sorry if the solution to this is obvious, I'm fairly new to unity.

avatar image SuperCats14 Alex_terrarian · Jan 07, 2021 at 04:14 AM 0
Share

Hi @$$anonymous$$_terrarian. Did you declare tiles in the Start function? If not do it like this: void Start() { tiles = new GameObject[maxX, maxY]; } I hope this fixes your problem.

avatar image Alex_terrarian SuperCats14 · Jan 07, 2021 at 04:51 AM 0
Share

Hi @SuperCats14 . I still cannot work out the issue, as I still receive the null reference error. is there are term for the usage of this GameObject list/array so I can search for online documentation?

Show more comments
avatar image
0

Answer by CmdrZin · Jan 07, 2021 at 06:26 AM

You could store the Tiles in a Dictionary. Use a string as a key.

 Dictionary<string, GameObject> tiles = new Dictionary<string, GameObject>();    

 string key = "r" + row.ToString() + "c" + col.ToString;

 tiles[key] = rawTile;
 

You can now get a tile back by generating a key knowing the row,col of the tile you need. The key could also be stored as the tile name in the TileData script.
Another note. You could attach the TileData script to the perfab blankTile so it's there when you use it to instantiate.

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 Alex_terrarian · Jan 07, 2021 at 08:48 AM 0
Share

Thanks @CmdrZin for another great method. Only downside to this is that you need to format back and forth between strings and integers in order to work with relative coordinates. (I'm trying to program $$anonymous$$esweeper for reference.)

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

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

Related Questions

changing a variable with a gui button 1 Answer

Grid-based building system not working 1 Answer

How to access varibles on a newly instantiated object 2 Answers

multiple instantiate object script help 2 Answers

Multiplying something.forward using a variable doesn't actually multiply, but multiplying by a number does. 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