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 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 which 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, which I assume is the problem. At this point, it's not a specific problem that I have to have everything in folders - but sooner or later this 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? This 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 something in a subfolder of that, it'd be Resources.Load("SubFolder/Cube")

To drag an object into the inspector (which 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

Aah yes, I think I recall reading about the resources folder - forgot about that. Thanks!

I've tried the drag and drop method of exposing the variable, but it doesn't seem to show in the inspector.

I've attached this: function update(){ var enemy : GameObject; transform.position.x=Random.Range(-4.5,4.5); obj=Instantiate(enemy,transform.position,transform.rotation); }

to the spawner, but it doesn't seem to show up in the inspector as a variable.

Thankyou very much for your response though.

avatar image Callum McIntyre · Jul 13, 2010 at 02:02 PM 0
Share

Aha. $$anonymous$$oved the variable out of the function, and it's shown up. Still figuring out what's wrong with the script itself because it won't work - but the variable is exposed now. :)

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

No one has followed this question yet.

Related Questions

How to properly load textures? 2 Answers

Can't drag assets from Project folder to Hierarchy/Scene 2 Answers

Unable to Drag and Drop from the Project pane into the Hierarchy pane in Unity 5.4.0b22 0 Answers

which one of the panels in unity has effect on the size of the project? 0 Answers

How to auto-navigate project hierarchy to asset by clicking in inspector? (Doesn't work anymore.) 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