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 tWayfarer · Apr 28, 2013 at 01:55 AM · prefablistresources.load

Loading prefabs into a list

I keep getting a null reference exception on the last line. I'm fairly sure the paths are all correct (and I've tried many different variations of the path in the actual Resources.Load command), the files are actually there in the right spot, but it doesn't work. (prefabTypes[] and levelType are both strings)

 DirectoryInfo dir = new DirectoryInfo("Assets\\Resources\\" + levelType);
 FileInfo[] info = dir.GetFiles(prefabTypes[i] + "*.prefab");
 
 List<GameObject> newList = new List<GameObject>();
 for(int j = 0; j < info.Length; j++)
 {
     print(levelType + "\\" + info[j].Name);
     GameObject obj = null;
     obj = Resources.Load(levelType + "\\" + info[j].Name) as GameObject;
     print(obj);
     newList.Add(obj);
 }
 prefabs.Add(prefabTypes[i], newList);
Comment
Add comment · Show 8
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 proandrius · Apr 28, 2013 at 02:01 AM 0
Share

Why are you using "\\" ?

avatar image proandrius · Apr 28, 2013 at 02:01 AM 0
Share

I mean two \

avatar image proandrius · Apr 28, 2013 at 02:04 AM 0
Share

Do you get null when you do this print(obj); ?

avatar image tWayfarer · Apr 28, 2013 at 02:05 AM 0
Share

"\\" is kind of like an escape character, so if the compiler tries to parse it it thinks you're telling it code. To actually have a backslash in your string, one of the ways to write it is with two "\\". Edit: even this comment box can't understand a single slash haha

avatar image tWayfarer · Apr 28, 2013 at 02:14 AM 0
Share

Yeah, I get null when I print it, too.

Show more comments

1 Reply

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

Answer by proandrius · Apr 28, 2013 at 02:22 AM

Ok so the problem is that Resources.Load loads from Assets/Resources folder, you can't load from C:/...etc. For example if you want to load AssetsResourcesForestStart1.prefab, you just write Resources.Load("AssetsResourcesForestStart1"); without path and extension.

Use Resources.Load(Path.GetFileNameWithoutExtension(info[j].Name));

Comment
Add comment · Show 24 · 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 proandrius · Apr 28, 2013 at 02:25 AM 0
Share

Oh, it stripped all \ and I thought AssetsResourcesForestStart1.prefab is a file name. But I guess you got the idea right?

avatar image proandrius · Apr 28, 2013 at 02:26 AM 0
Share

stripped all "\\" (damn these slashes)

avatar image proandrius · Apr 28, 2013 at 02:29 AM 0
Share

Anyway, to sum-up, you need to get only a path from Resources folder (not including Resources) and file name without extension. If you have file in Assets//Resources//Level1//file.prefab, so to Resources.Load you need to give only "Level1\\file".

avatar image tWayfarer · Apr 28, 2013 at 02:37 AM 0
Share

It's still not working. I tried "obj = Resources.Load(levelType + "\\" + Path.GetFileNameWithoutExtension(info[j].Name)) as GameObject;", and again with a slash in front of levelType, but it still comes up null. The print gives out the right folder before it, too.

avatar image proandrius · Apr 28, 2013 at 02:38 AM 0
Share

Can you do print(levelType + "\\" + Path.GetFileNameWithoutExtension(info[j].Name)); and let me know what you get.

Show more comments

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

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

Related Questions

A node in a childnode? 1 Answer

How do I Instantiate a prefab in a list? 0 Answers

Saving a level with the help of prefabs 3 Answers

Resources.Load() What has to be in Resources Folder 1 Answer

Populating a list with different items in different clones of the same prefab. 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