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 sk00ter · Feb 05, 2015 at 12:50 PM · instantiateprefabsstartupindexing-array

Instantiating Prefabs into an Array with Indexing

I'm drawing a room of public prefab cubes (that are actually made up of 6 Quads[Top, Bottom, Front, etc.]), each cube is 1x1x1 and the size of the room is 1 unit high on the Y value but the Z and X are determined by a public int input called roomsize (Ex. 64x64).

My goal here is to index these prefab cubes as they are drawn in so I can reference them later in Raycasting. For example, if I want to select the "Top" quad of "prefabCube_[index]" I can simply make a hit.collider.prefabCube_1.Top, or something-like-that.

I've kind of hit a wall, I'm not unfamiliar with C# but I am new to Unity. Can anyone point me in the right direction?

Code Below:

 public class createCube : MonoBehaviour {
 
     public GameObject prefab;
     public int roomSize;
 

     // Use this for initialization
     void Start () {
         for (int i = (-1*roomSize); i < roomSize; i++) {
             for (int j = (-1*roomSize); j < roomSize; j++) {
                 GameObject tileMap = Instantiate(prefab, new Vector3(i, 0, j), Quaternion.identity) as GameObject;
             }
         }
     }
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 Mmmpies · Feb 05, 2015 at 08:12 PM

This is what I do for menu items when I want to dynamically create spell menus. I use one scroll panel and instantiate different types of spell so I store the instantiated in a list so I can destroy the old ones when the menu changes:

 private List<GameObject> localMenuItemList = new List<GameObject>();
 
 
 // then when I instantiate
 GameObject newItem = Instantiate(itemPrefab) as GameObject;
 
 
 // I edit the properties of that prefab to fit the spells
 // when all that's done I add to the List
 localMenuItemList.Add(newItem);
 
 
 // when I want to clear the list:
 if(localMenuItemList.Count > 0)
         {
             for(int i = 0; i < localMenuItemList.Count; i++)
             {
                 Destroy(localMenuItemList[i]);
             }
         }
         localMenuItemList.Clear();

Oh almost forgot, you need:

 using System.Collections.Generic;

To use Lists

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

Instantiate an object in a prefab? 1 Answer

Instantiate only certain objects within a prefab 1 Answer

Invisable Projectile; Instantiate at Collisions 2 Answers

instantiate a prefab from array and then changing it 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