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 Nochek · Mar 30, 2011 at 11:04 PM · instantiateprefabarray

Assigning Instantiated Tiles to Array

Here is what I am trying to do (here at least). I want to create a playing board called World, and have it made up of separate cubes (which are actually planes, but whatever). I have one prefab thats a generic cube, then a couple cubePrefabs that are different types. At runtime, I want to board to be created, then set all the cubes into an array. Then, during Update functions I will (either destroy or place over the top) add the cubePrefabs to change the World. I have created the world, and the cubes, and they all get named, but I can't figure out how to set each named Cube into an Array that I can call on to change during update. I am assuming I am calling in the array function at the wrong time, but I am not sure where to stick it. Any help would be greatly appreciated.

public class MakeDaCubes : MonoBehaviour {

 public int board_size_x_;
 public int board_size_z_;
 public Transform cube_prefab_;

 public GameObject[] cubePrefabs;
 public Transform[] boardCubes;

 // Use this for initialization
 void Start()
 {
     //Creates and instantiates the board
     GameObject board = new GameObject();
     //Names the board World
     board.name = "World";
     //math to make X variable grow to board_size_x
     for (int x = 0; x < board_size_x_; x++)
     {
         //math to make Y variable grow to board_size_y
         for (int z = 0; z < board_size_z_; z++)
         {
             Transform cube = (Instantiate(cube_prefab_, new Vector3(x, 0, z), Quaternion.identity) as Transform);
             cube.name = "Cube" + x + "," + z;
             cube.parent = board.transform;

             Transform[] boardCubes = new Transform[]{cube};



         }
     }        
 }

New Code resulting in multiple debug logs being posted:

public class MakeDaCube : MonoBehaviour {

 public int board_size_x_;
 public int board_size_z_;
 public Transform cube_prefab_;

 public GameObject[] cubePrefabs;
 public Transform[] boardCubes;

 private int b;
 // Use this for initialization
 void Start()
 {
     Transform[] boardCubes = new Transform[(board_size_x_ * board_size_z_)];
     //Creates and instantiates the board
     GameObject board = new GameObject();
     //Names the board World
     board.name = "World";
     //math to make X variable grow to board_size_x
     for (int x = 0; x < board_size_x_; x++)
     {
         //math to make Y variable grow to board_size_y
         for (int z = 0; z < board_size_z_; z++)
         {

             Transform cube = (Instantiate(cube_prefab_, new Vector3(x, 0, z), Quaternion.identity) as Transform);
             cube.name = "Cube" + x + "," + z;
             cube.parent = board.transform;

             //pushes tiles into boardCubes array
             boardCubes[b++] = cube;

             //iterates through each newCube and debugs its name
             foreach (Transform newCube in boardCubes)
                 {
                     Debug.Log("Added Cube: " + cube.name);
                 }

             //iterates through each boardCubes and debugs its value
             //for (int i = 0; i < 10; i++)
             //    Debug.Log(boardCubes[x]); 
         }
     }        
 }

}

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

Answer by DaveA · Mar 30, 2011 at 11:25 PM

You might look into using Array or List, but I think what you want to basically do is stuff them in the array as you go, something like:

cube.parent = board.transform;
boardCubes[b++] = cube;

where b starts as 0. Using the array type you have, you may need to pre-size it to the number of cubes you'll be making (board_size_x_ * board_size_z_)

Comment
Add comment · Show 3 · 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 Nochek · Mar 31, 2011 at 03:31 AM 0
Share

Thats apparently what I needed to push them into the Array, thank you.

I added the necessary code, and then did this:

            foreach (Transform newCube in boardCubes)
                 {
                     Debug.Log("Added Cube: " + cube.name);
                 }

but I get back 40~ or so Debug.Log's per cube. (ie, Added Cube: Cube0,0 40 times, then Cube0,1 another 40) is this just how Debug.Log outputs in this situation or am I actually running the code too much?

avatar image DaveA · Mar 31, 2011 at 07:47 AM 0
Share

That sounds like too much

avatar image DaveA · Mar 31, 2011 at 07:48 AM 0
Share

Can you edit your post to use your new code?

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

No one has followed this question yet.

Related Questions

Array of prefabs with different sizes stick to each other 0 Answers

Prefabs instantiated from an array are keeping their public int value 1 Answer

Parenting an instantiated prefab. 1 Answer

Array of prefabs 0 Answers

OverlapSphere for parallel arrays 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