Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
14
Question by Jens T · Aug 06, 2010 at 03:36 PM · instantiateprefabcomponentstreaminginstantiation

Instantiate prefab wihtout dragging prefab into gameObject script

I have a large volume of prefabs, and I want the user to able to select from a menu different names. These names each correspond to a specific prefab. When this happens, a specific GameObject is selected, and I would like to instantiate a prefab under that GameObject local Transform. (I am building a machine, and for each part there is a list of other possible parts than can be attached, stoed in a database. The part library is rather big. Think Toons or "The Incredible Machine" :-) ).

Later I would like to stream these components, and this is the basis for this problem.

var cube : Transform;

function Start () { for (var y = 0; y < 5; y++) { for (var x = 0; x < 5; x++) { var cube = Instantiate(cube, Vector3 (x, y, 0), Quaternion.identity); } } }

The current code is taken from the instantiation tutorial available at http://unity3d.com/support/documentation/Manual/Instantiating%20Prefabs.html

For this to work, I am required to put the above script into a gameObject, and then Drag the Prefab into the Cube variable slot for that script component in the inspector. This means I have to have access to all the prefabs. I would like to achieve this without the dragging. This is because the plan is to put new assets (prefabs) in my online database as this collection grows, and have the system automatically donwload (from an URL) the prefabs as needed.

Is this possible, or have I completely misunderstood the concept of Prefabs? Is there a way that I can set this variable to a streamed asset using the WWW class? ( I found this thread :http://answers.unity3d.com/questions/17376/reconnect-programmatically-instantiated-prefab but I did not understand it completely, although it might be what I am looking for).

Help is really appreciated.

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

9 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by AliAzin · Aug 06, 2010 at 05:24 PM

you can export your prefabs to AssetBundles and call your prefabs through there. see Character Customization project.

Comment
Add comment · Show 2 · 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 MountDoomTeam · Sep 22, 2012 at 05:03 PM 1
Share

i can't believe that the Unity people thought that an interesting project for us 20 30 year olds to learn from was dressing up barbies. (off topic) they are not even adult barbies with prober boobs, its dressing up 7 year olds in pink and yellow. TEDIOUS!

avatar image Krahgnatokz · Feb 06, 2015 at 12:45 AM 0
Share

By any chance, was the project remade into the "robot lab"?

avatar image
22

Answer by Thet Naing Swe · Oct 08, 2010 at 02:29 AM

If your prefabs are located in the Resources Folder, then you can load your prefab in that ways.. it has been tested and works.

C#Version

GameObject testPrefab = (GameObject)Resources.Load("/Prefabs/yourPrefab");

Comment
Add comment · Show 2 · 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 $$anonymous$$ · Oct 07, 2013 at 11:23 AM 0
Share

Exactly what I do, thumbs up.

avatar image gm2008 · Aug 30, 2014 at 09:07 AM 1
Share

The path should be relative to the Assets/Resources folder. I have only got a success with this:

 GroundTile = (GameObject) Resources.Load("Prefabs/$$anonymous$$apTiles/GroundTile", typeof(GameObject));
avatar image
2

Answer by pravusjif · Oct 27, 2012 at 11:51 PM

I tried this in several ways but i could only make it work this way:

GameObject codeInstantiatedPrefab = GameObject.Instantiate( Resources.LoadAssetAtPath("Assets/Prefabs/myPrefab.prefab", typeof(GameObject)) ) as 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 gm2008 · Aug 30, 2014 at 08:53 AM 1
Share

Resources.LoadAssetAtPath is for Editor only though. The documentation says,

"Returns a resource at an asset path (Editor Only). This function always return null in the standalone player or web player. This is useful for quickly accessing an asset for use in the editor only."

avatar image
1

Answer by Happy-Zomby · Aug 20, 2014 at 09:30 AM

Hi, For the Resources.Load I don't believe you need the prefab extension... just need to make sure you don't have a "/" at the beginning of your path... You can also use a string/list to generate it. E.g. var tempObject = Instantiate(Resources.Load("Prefabs/Units/"+loadData.animalNames[i])) as GameObject;

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

Answer by Ziron999 · Feb 07, 2014 at 04:31 PM

Resources.Load is the best way to do this...this is how you do it in detail:

 Instantiate(Resources.Load<GameObject>("NameOfPrefab"WITHOUT(.prefab)"")) as GameObject;

Folder in assets folder must be created called Resources. Loads on both builds and in editor! No need for assetbundle that is a pro only feature anyway!

Comment
Add comment · Show 2 · 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 Clonkex · Nov 05, 2014 at 01:55 AM 1
Share

LoadAssetAtPath is only allowed in the editor and won't work in a standalone build or the web player.

avatar image Ziron999 · Feb 07, 2015 at 01:55 AM 0
Share

I'm not using loadassetatpath???

  • 1
  • 2
  • ›

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

10 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

Related Questions

How to make sprites instantiate based on randomly generated integer? 1 Answer

Access prefab component without instantiating 1 Answer

Why is it important to create an empty gameobject for my prefabs? 0 Answers

Instantiating in Awake() v Setting up prefab instances in Editor performance difference? 1 Answer

VUFORIA: Playing an animation for a instanced model? 2 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