Search Unity

{SOLVED} Adding Prefab as GameObect Variable WITHOUT dragging method via C#

Discussion in 'Editor & General Support' started by Shykeas, Sep 28, 2016.

  1. Shykeas

    Shykeas

    Joined:
    Apr 11, 2015
    Posts:
    145
    *~WITHOUT USING RESOURCES FOLDER~*

    Yes, I've bold, underlined and italicized it. WITHOUT ANYTHING THAT HAS TO DO WITH "Resource.Load" "Resource.ANYTHING"

    So now that we have that straightened; I do wish to assign a Prefab FROM A FOLDER IN MY ASSETS as the variable.

    This is easy to do by dragging and dropping BUT I CANNOT HAVE THIS. When I build it, dragging and dropping is not an option, but I need the prefab that is selected to change, depending on whatever I decide. (irrelevant.)

    All I want is for:

    */

    Public GameObject Obj;

    Obj = "Assets/Whatever/File.prefab";

    /*

    How is this done? Especially without using Resources. If it is ABSOLUTELY THE ONLY WAY, then I'll take it. But if there is ANY OTHER, then please do share.
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    The only way to get an asset that doesn't already exist in the scene is to load it through the resources folder.

    why?
    When a project is built, all the data in the assets folder get merged into the executable, and can no longer be accessed through a folder structure, as normal. Resources is specially made so you can get assets out of the executable using their filepaths.

    What can you do?
    You can have multiple resources folders. For example: "Assets/Enemies/Resources" and "Assets/Obstacles/Resources". Another thing you could do is turn your assets into asset bundles and load those at runtime.
     
  3. Shykeas

    Shykeas

    Joined:
    Apr 11, 2015
    Posts:
    145
    Ok. I appreciate this. I feared that much and was going to test my non-resource solution (Which I never found) in build mode anyways in skeptical eyes.

    Anyways what is going on now is every time I load something there it is returned as Null.

    Code (CSharp):
    1. if (Input.GetKeyDown(KeyCode.P))
    2.         {
    3.             PrefabUtility.CreatePrefab ("Assets/Resources/LevelData.prefab", GameObject.Find ("LevelData"));
    4.  
    5.         }
    6.         if (Input.GetKeyDown(KeyCode.L))
    7.         {  
    8.             LevelSav = Resources.Load("LevelData") as GameObject;
    9.  
    10.             Debug.Log (Resources.Load(""));
    11.         }

    What happens is LevelSav is a GameObject variable. I press L and it should load LevelData.prefab from the Resources folder. Then assign it so I can then instantiate it that way. problem is...
    I press P, I see the prefab become created in Assets/Resources as it's self.
    I then press P and Debug simple says "Null". :'(
    It isn't getting Level Data.
     
  4. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    I'm afraid the PrefabUtility belongs to the UnityEditor namespace. You cannot use UnityEditor code in a build.

    That said, you could do this at runtime:
    Code (CSharp):
    1. GameObject myPrefab;
    2.  
    3. void Update()
    4. {
    5.     if (Input.GetKeyDown(KeyCode.P))
    6.     {
    7.         myPrefab = GameObject.Find ("LevelData");
    8.     }
    9.     if (Input.GetKeyDown(KeyCode.L))
    10.     {
    11.         Instantiate(myPrefab);
    12.     }
    13. }
    So instead of creating a prefab at runtime, you just store a reference to the GameObject. You can use Instantiate to copy that GameObject later.
     
  5. Shykeas

    Shykeas

    Joined:
    Apr 11, 2015
    Posts:
    145
    That is what I feared most. :(

    What is the use of myPrefab if I cannot save it? What would be the most efficient way to save a GameObject (Which was modified in build, is why one wants to save it) to reference OUTSIDE of the scene, so if I wanted to load the modified GameObject, I can instantiate it?