• 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 Callum McIntyre · Jul 13, 2010 at 01:03 PM · loadprojectresourceshierarchy

Loading Resources from folder in Project to Hierarchy

It sounds like a really simple question, but it's a problem I'm having and I've been unable to find a response yet.

I'm currently doing a few tests to set up a tower defence game eventually - and I've put a test prefab called "Cube" in a folder called Enemies, in project view. I have tried using the following code:

object=Resources.Load("Cube");
transform.position.x=Random.Range(-4.5,4.5); 
obj=Instantiate(object,transform.position,transform.rotation);

It's attached to a spawner object w$$anonymous$$ch will move to a random x location and create the Cube prefab there. It's quite obvious that the Cube part is the problem - and that's definitely its name.

However, it's in a folder called Enemies, w$$anonymous$$ch I assume is the problem. At t$$anonymous$$s point, it's not a specific problem that I have to have everyt$$anonymous$$ng in folders - but sooner or later t$$anonymous$$s organisation will be essential so I may as well nail it now.

Could anybody please help me with how to reference an object in a folder?

On a side note, how would I have it let me drag the prefab in via the inspector? T$$anonymous$$s would probably be a much easier method.

Thanks very much, Callum.

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
3
Best Answer

Answer by Mike 3 · Jul 13, 2010 at 01:25 PM

You must put the items inside the Resources folder (or a subfolder of it)

In your case, /Project/Assets/Resources/Cube will be loaded by Resources.Load("Cube")

If you want to load somet$$anonymous$$ng in a subfolder of that, it'd be Resources.Load("SubFolder/Cube")

To drag an object into the inspector (w$$anonymous$$ch is in fact easier), you would just make a public variable for the type of object

In your case:

c#

public GameObject cube;

js:

var cube : GameObject

You'd then just drag and drop into the inspector

Note - you don't have to make it GameObject, it can be any type you have in your prefab, including scripts, AudioSource, etc etc

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 Callum McIntyre · Jul 13, 2010 at 01:46 PM 0
Share
avatar image Callum McIntyre · Jul 13, 2010 at 02:02 PM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Resources.Load folders path? 2 Answers

C# Error when trying to load Texture at runtime 1 Answer

A method for locking a prefabs instance from being modified? 1 Answer

Want to start blank project and transfer old into it. What problems can I expect? 1 Answer

load in Hierarchy 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