Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

For every prefab in my resource folder, I want that prefab added to an array. How would I do this?

Discussion in 'Scripting' started by keenanwoodall, Jul 22, 2014.

  1. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I'm basically trying to do what the title says. Btw, I'm doing it in javascript. If you know how to do it in c# and not javascript, I'd still like to know cuz I can probably translate :)
     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Haven't checked this but I use some code like this ( it's in c# )

    Code (csharp):
    1.  
    2. public Transform[] playerPrefabArray;
    3.  
    4. arrayName = new Transform[Resources.LoadAll("FolderInResources", typeof(Transform)).Length];
    5.  
    6. foreach(Transform d in Resources.LoadAll("FolderInResources", typeof(Transform)))
    7. {
    8.     arrayName[count] = d;
    9.     count++;
    10. }
    11.  
     
  3. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    thanks man, I'll try and make it work. Also, on a side note, this script is so that I can instantiate my custom prefabs easily without having to write a script that holds each object and apply it to whatever gameObject I want to Instantiate them from. I'm basically writing my own public static class, similar to unity's Input class
     
  4. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    I'm trying to make it work for my script but i can't figure it out. If you have the time to look at my script, that'd be great :)
    Code (js):
    1.  
    2. public static class CoolTools
    3. {
    4.     static var objectArray : GameObject[] = [Resources.LoadAll("KeenansCoolResources/KeenansCoolPrimitivePrefabs" as GameObject).Length];
    5.     for (obj : GameObject in Resources.LoadAll("KeenansCoolResources/KeenansCoolPrimitivePrefabs" as GameObject))
    6.     {
    7.         objectArray[count] = obj;
    8.         count ++;
    9.     }
    10.     enum Primitives
    11.     {
    12.        
    13.     };
    14.     public static function CreatePrimitive (primitiveName : Primitives) : GameObject
    15.     {
    16.        
    17.     }
    18. }
     
  5. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Don't know javascript, but this may help http://docs.unity3d.com/ScriptReference/Resources.LoadAll.html , then select the JS tab.

    Also you are putting your folder in the Resources/ folder in the root of the project, that is a hardcoded location for loading gameobjects in unity

    So path for your project would be Resources/KeenansCoolResources/KeenansCoolPrimitivePrefabs and you don't include the Resource folder in the LoadAll path....
     
  6. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    595
    Yeah i know, I don't know why I added that