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 10, 2013 at 11:23 PM · c#listresources

Problem with getting resources and adding to list. (c#)

Hello, I'm having some trouble with what I hoped to be a reasonably easy piece of code. My intention is for it set a GameObject variable to be a file ending in .prefab from a resources folder and then changing the name to be a number before adding it to a list and increasing said number until there's no remaining GameObjects in the folder.

For some reason, it doesn't want to work properly - but I'm getting no errors, so I have no clue what's gone wrong.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PrefabSetup : MonoBehaviour {
 
     public static List<GameObject> prefabRooms = new List<GameObject>();
     public static int prefabCount = 0;
 
     void Start () {
 
         int objectNumber = 0;
         string objectNumberAsString;
         bool prefabsLoaded = false;
 
         while (prefabsLoaded == false) {
             GameObject currentObject = Resources.Load("*.prefab") as GameObject;
 
             System.Threading.Thread.Sleep (50);
 
             if (currentObject == null) {
                 Debug.Log ("currentObject = null");
                 prefabsLoaded = true;
             } else {
                 Debug.Log (currentObject.ToString ());
                 objectNumberAsString = objectNumber.ToString ();
                 currentObject.name = objectNumberAsString;
 
                 Debug.Log ("Set To: " + currentObject.ToString ());
                 prefabRooms.Add (currentObject);
 
                 objectNumber++;
                 prefabCount++;
             }
         }
     }
     
     void Update () {
     
     }
 }


Any help would be appreciated. The reason I'm doing it this way and not simply doing a GameObject array with a Resources.LoadAll is I eventually want to have a progress bar or loading percentage and I want it to visibly go through every file and load it one after the other.

Comment
Add comment · Show 2
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 Statement · Dec 10, 2013 at 11:31 PM 0
Share

Never seen that you can pass "*.prefab" to Resources.Load. Does that actually work or is this what is going wrong for you?

avatar image Tanksy · Dec 10, 2013 at 11:44 PM 0
Share

I was told that the star meant anything to the code.

1 Reply

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

Answer by flaviusxvii · Dec 10, 2013 at 11:39 PM

So I'm pretty sure this:

 Resources.Load("*.prefab")

can't possibly work the way you intend. That'll return null right off the bat. You'll get "currentObject = null" in your console, and then the loop is over.

Comment
Add comment · Show 4 · 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 10, 2013 at 11:43 PM 0
Share

How so? Is that not to load anything that with .prefab?

avatar image flaviusxvii · Dec 10, 2013 at 11:45 PM 0
Share

http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.

There's no reason to think it would respect that wildcard syntax.

avatar image Tanksy · Dec 11, 2013 at 02:32 AM 0
Share

So how can I load a resource that can have any name but must be a prefab? Googling it isn't helping me.

avatar image Tanksy · Dec 11, 2013 at 03:49 PM 0
Share

Alright well I'm going to try a different method, thanks for the help.

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

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

A node in a childnode? 1 Answer

can somebody help for the algorithm or code how implement this crossword math? 1 Answer

Combine duplicates in a collection of class objects 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