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 Ace_Oxide · Jan 10, 2017 at 09:43 PM · scripting beginner

How can I populate a list of GameObjects from the prefabs folder?

Basically I'm trying to simplify my project to protect my own personal sanity... I have a mega crap ton of different style objects to create, all will be used by one control script at different times. Basically I'm curious if instead of having to drag-and-drop each onto the list in the inspector if I can have it so when the script is Awake it automatically populates the list with ALL the GameObjects in Assets/Prefabs/Cubes...

I searched on Bing quite a bit but came up short on a specific answer... Sorry if it's a repeat question.

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
0

Answer by HenryStrattonFW · Jan 10, 2017 at 11:58 PM

The short answer, is no. At runtime (outside of the editor) the concept of the location "Assets/Prefabs/Cubes" simply does not exist.

However there are ways you can still achieve what you want. It still involves having all of your prefabs attached to your object in the inspector, however that doesn't mean you have to do that by hand. You can write and editor script (or just a method on your objects script that is contains in UNITY_EDITOR define check) that scans your folder of choice and searches it for all valid objects that you want (either by type, naming convention, etc). and then it sets the value on your object.

Here is small example of a script where I want to get all texture objects from a specific folder. It was written using Unity 5.5 but I'm fairly sure the asset database calls/filters are the same in most recent versions.

 #if UNITY_EDITOR
 using UnityEditor;
 #endif
 
 using UnityEngine;
 using System.Collections.Generic;
 
 
 public class TextureCollection : MonoBehaviour
 {
     [SerializeField]
     private Texture2D[] m_Textures;
 
 #if UNITY_EDITOR
     [ContextMenu("Find Textures")]
     private void _FindTextures()
     {
         List<Texture2D> lTextures = new List<Texture2D>();
 
         string[] lGuids = AssetDatabase.FindAssets("t:Texture2D", new string[] { "Assets/Test" });
 
         for (int i = 0; i < lGuids.Length; i++)
         {
             string lAssetPath = AssetDatabase.GUIDToAssetPath(lGuids[i]);
             lTextures.Add(AssetDatabase.LoadAssetAtPath<Texture2D>(lAssetPath));
         }
 
         m_Textures = lTextures.ToArray();
     }
 #endif
 }
 

Then you save your scene or prefab (depending on your object) and then the objects in question will be included in your build assuming the object with this script on is included as a resource or in a scene, and your main object will have all the references it needs ready to go when it starts up.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using singleton & DontDestroyOnLoad to keep a menus state. 1 Answer

How to load different sprites on a panel based on player level 1 Answer

,How to get the script of an element? or the GameObject of an Component? 1 Answer

Make object active and inactive repeatedly in a specific way? 2 Answers

In part assembly project, How can I check the part status whether it is fixed to correct place or not ? 0 Answers


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