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 Tanksy · Dec 11, 2013 at 05:30 PM · c#arrayresources.load

Keep getting null when using Resource.load with a path.

Specifically, the path is taken from an Array. I'm not an expert on C# so I can't easily see what is wrong here;

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PrefabSetup : MonoBehaviour {
 
     public static List<GameObject> prefabRooms = new List<GameObject>();

     string resourcesFolder;
     string resourcesFilter = "*.prefab";

     int objectNumber = 0;
     string objectNumberAsString;

     GameObject currentObject = null;
 
     void Start () {
         // Sets resourcesFolder to be the correct path.
         resourcesFolder = Application.dataPath + "/Resources/Prefab_Rooms/";
     }
 
     void Update () {
         // Creates the prefabFiles array.
         string[] prefabFiles = System.IO.Directory.GetFiles(resourcesFolder, resourcesFilter);
 
         for (int i = 0; i < prefabFiles.Length; i++) {
 
             string path = prefabFiles[i];
 
             currentObject = Resources.Load(path) as GameObject;
             Debug.Log (currentObject.ToString ());
             
             objectNumberAsString = objectNumber.ToString ();
             currentObject.name = objectNumberAsString;
             Debug.Log ("Set To: " + currentObject.ToString ());
             
             prefabRooms.Add (currentObject);
             objectNumber++;
         }
     }
 }


What I'm trying to do is make it so there's a "Prefab_Rooms" folder that you can simply drop .prefab files into and then when the game is run, the code will get each file and add them to a list which will be used throughout the game. I want to do this as easily as possible, whilst still loading each file individually rather than use Resources.LoadAll (So that I can make it visible to the user that each file is being loaded successfully.)

Does anyone have any ideas what is going wrong and can you please help? I keep getting the NullReferenceException and I don't understand why.

(This is similar to my previous question, but since that question I've changed the code but still to no avail)

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

Answer by robertbu · Dec 11, 2013 at 05:58 PM

You have a couple of problems here. First, GetFiles() returns a full path. Resources.Load() needs a relative path to the 'Resources' folder. So you will need to do some processing on the strings returned by GetFiles() to have something appropriate for Resources.Load(). The second problem is that the 'Resources' folder does not exist in a build. So if you are tying to run this script in anything but the editor it will fail.

Assuming you need a runtime solution, you could make prefabFiles a public class variable and write an editor script that populates the list in the editor. At runtime, you would then just have to cycle through the list.

Comment
Add comment · Show 3 · 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 Tanksy · Dec 12, 2013 at 07:29 PM 0
Share

Thank you for the help. I guess that - for what I'm trying to do - using Resources.load isn't such a good idea? I'm not sure what I could do ins$$anonymous$$d, but I will look into some possibilities.

I need it to work in builds outside of the editor. What exists in place of a resources folder? Do resources just get put into the _Data folder?

avatar image robertbu · Dec 13, 2013 at 01:18 AM 0
Share

Resources.Load() is fine. It works in a build outside the editor as long as the files you are adding are at edit time. GetFiles()does not work because the files inside Resources.Load() get folded into a binary file. So What I'm suggesting is that you create an editor script that processes "/Resources/Prefab_Rooms/" and produces an array of names for the game objects to be loaded. These strings will be serialized and be available in the build, so you can then load them in a loop and report the progress to the user. Of course this will not work if you are trying to allow user to add files at the runtime of a build.

avatar image Tanksy · Dec 17, 2013 at 09:14 PM 0
Share

Well not allowing them to add files whilst the build is running is fine, I wouldn't of wanted them too anyway. I just would like a way of allowing users to put prefab files into a folder which then get loaded and "set up" for use when the game is loaded. I think I understand what you're saying, so I'll try looking up ways to do it.

$$anonymous$$y coding knowledge isn't great, but this will hopefully be good for learning.

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Set Current Array GameObject to Active 1 Answer

How to check for an empty array? 1 Answer

Iterating through multidimensional arrays 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