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
1
Question by p24601 · Jul 03, 2012 at 03:43 PM · destroyprefabsprojectcreateiterate

How to iterate all prefabs in project and create them in scene

Hi there, I'm very new to Unity Programming so I'm not sure what's possible and what is not. I didn't see this question asked before so here goes.

What I am looking to do is iterate through all the prefabs in a particular directory in my project and create/destroy them in scene so some operations can be performed.

essentially

var directory = "\assets\objects\items\";

foreach(var prefab in directory) {

Create(prefab)

// do something with prefab

Destroy(prefab)

}

Is this possible? Or can you only iterate objects already in a scene?

If so, how would you do it, and how do you create/destroy those prefabs?

Comment
Add comment · Show 1
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 p24601 · Jul 06, 2012 at 01:22 PM 0
Share

So this is ended up being the solution I went with:

IEnumerable>string< assetFolderPaths = AssetDatabase.GetAllAssetPaths().Where(path => path.EndsWith(".prefab") && path.Contains(pathFilter));

IEnumerable>string< assets = assetFolderPaths.Select(item => AssetDatabase.LoadAssetAtPath(item, typeof(GameObject))).Cast>GameObject<().Where(obj => obj != null);

int i = 0;

foreach (GameObject obj in assets) {

obj.rigidbody.useGravity = false;

var newObject = Instantiate(obj, new Vector3(i++ * 100.0f, 0, 0), Quaternion.identity) as GameObject;

_items.Add(newObject);

}

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dannyskim · Jul 03, 2012 at 07:09 PM

Take a look at this following link:

http://answers.unity3d.com/questions/234935/how-do-i-enumerate-the-contents-of-an-asset-folder.html

This shows you how to iterate through the returned objects in a directory. After you get the returned data paths, you could simply:

 Instantiate( Resources.Load( "assetDataPathHere" ) );

And destroy the cached object when you need to accordingly.

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 keenanwoodall · Jul 15, 2014 at 08:56 PM 0
Share

How would I load the prefab without Unity telling me that a String isn't an object?

avatar image
1

Answer by Luci85 · Jul 03, 2012 at 07:20 PM

An easy solution is assigning all prefabs with a tag and just gather all gameobjects with the same tag:

 var gameobjects:GameObject[] = GameObject.FindGameObjectsWithTag("PrefabTag");

And then loop through all gameobjects:

 for(var gameobject:GameObject in gameobjects)
 {
 Create(gameobject)
 // do something with prefab
 Destroy(gameobject)
 }
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 dimosdennis · Sep 22, 2015 at 02:41 PM 0
Share

This will only work for prefabs already instantiated in the scene unfortunately.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a cloned particle emitter fire once only 2 Answers

How to create object using onCollisionEnter trigger function ? 1 Answer

Unity 2021 (Blank Preview Icons and shaders in Project browers are pink but work.) 0 Answers

DestroyImmediate Prefabs 3 Answers

Unable to create project. Ver 3.0.0 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