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 BluesyPompanno · Oct 24, 2018 at 06:01 PM · transformarrayrandomgeneration

How to store Transform in an Transform array ?

Hello. I am trying to code a little "world" generation system.


What I have is when you start new game world of a size you choose will be generated. The world is created from cubes (1x1x1).


What I can't figure out Is how do I put these created blocks into an array.


For example -> I have a world with a size of 10. So that means 10 blocks will be created on the X and Z coordinate. ( 100 blocks in total ) I want each block to be stored in and array.From this array the game will choose random number. So let's say I have 100 blocks and the game will choose 35 blocks where the trees will be created and 65 blocks will be empty.


Can anybody help me please ?


Here is my code for " world" generation.

    public Transform groundBlock;
     public Vector2 mapSize;
 
     public Transform[] allGroundBlocks;
 
     // Use this for initialization
     void Start () {
         GenerateMap();
     }
 
     void GenerateMap()
     {
         string holderName = "Generated Map";
         if (transform.Find(holderName))
         {
             DestroyImmediate(transform.Find(holderName).gameObject);
         }
 
         Transform mapHolder = new GameObject(holderName).transform;
         mapHolder.parent = transform;
 
         for(int x = 0; x < mapSize.x; x++)
         {
             for(int y = 0; y < mapSize.y; y++)
             {
                 Vector3 tilePosition = new Vector3(-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y);
                 Transform newTile = Instantiate(groundBlock, tilePosition, Quaternion.Euler(Vector3.right)) as Transform;
                 newTile.parent = mapHolder;
             }  
         }
     }
 }
 

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 Bleakmountain50 · Oct 24, 2018 at 11:51 PM

If you use a List instead of an Array you'll have a much easier time

all I did was swap out public Transform[] allGroundBlocks;

to public List<Transform> allGroundBlocks; and added in a part so any instantiated object is added to the list

here's the completed code:

 public Transform groundBlock;

 public Vector2 mapSize;

 public List<Transform> allGroundBlocks;

 // Use this for initialization
 void Start()
 {
     GenerateMap();
 }

 void GenerateMap()
 {
     string holderName = "Generated Map";
     if (transform.Find(holderName))
     {
         DestroyImmediate(transform.Find(holderName).gameObject);
     }

     Transform mapHolder = new GameObject(holderName).transform;
     mapHolder.parent = transform;

     for (int x = 0; x < mapSize.x; x++)
     {
         for (int y = 0; y < mapSize.y; y++)
         {
             Vector3 tilePosition = new Vector3(-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y);
             Transform newTile = Instantiate(groundBlock, tilePosition, Quaternion.Euler(Vector3.right)) as Transform;
             newTile.parent = mapHolder;
             allGroundBlocks.Add(newTile);
         }
     }
 }

}

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 BluesyPompanno · Oct 25, 2018 at 06:16 PM 0
Share

Holy moly. It was so easy and I couldn't see it. It works ! :D Thank you.

avatar image
0

Answer by ADustyOldMuffin · Oct 25, 2018 at 12:13 AM

If you wish to store them specifically in an array you will have to declare the size of the array. So in example at the beginning of your generate method put

 allGroundBlocks = new Transform[(int)mapSize.x, (int)mapSize.y];

And you'll have a 2d array the size of the map, so when you wish to store your objects you will store them in the array with their x and y cords like so.

 allGroundBlocks[x, y] = gameObject.transform;

You'll notice I have to use (int) because an array can only take integers in it's constructor, and you'll have to do the same with your transforms being stored in the array. In the future though I would not store the map size as a Vector2 I would store it has a length and width like a square, and then assuming it starts at 0 you can give them cords based on that.

EDIT: I added an example for adding to the array

Comment
Add comment · 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

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

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

Related Questions

problem extracting transform from an array 1 Answer

random number generator problem 1 Answer

Spawn object at a random predeterminated(transform) position 1 Answer

move enemy to random transform array 2 Answers

Clear 1 field of Transform array 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