Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate Prefab not working after Build

Discussion in 'Scripting' started by DerDree, Apr 18, 2012.

  1. DerDree

    DerDree

    Joined:
    Jan 16, 2012
    Posts:
    6
    Hi,

    in my current project I instantiate a prefab with a script. The script has a public prefab variable and I dragged my prefab in the editor to this script.
    It all works great, but when I build my project and start the build the prefab does not appear. First I thought it might be, because the prefab is instantiated at the start and the script may be not ready in the build. So I made a button for it but it still does not work.

    Can someone help me please?

    Thanks in advance :)
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Show your code.
     
  3. DerDree

    DerDree

    Joined:
    Jan 16, 2012
    Posts:
    6
    Its a very long code but the instantiation itself is pretty basic and like in the unity tutorial.

    But I just got an idea what the problem could be:
    My shape of the level (and so the prefabs) is build according to a .txt-file. I load this file with a streamreader using this as a path to the file:
    Code (csharp):
    1. path = Application.dataPath + "/" + fileName;
    Can it be, that the path isnt correct anymore after building the project? How can I define a "correct" path?

    EDIT:
    Just tried it and the problem is indeed the .txt-file. I have to copy the file manually in die data-folder of the build project.
    Can this be done automatically during the build somehow?
     
    Last edited: Apr 18, 2012
  4. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    Replace path with a hardcoded value and try then. This way if it works you will know the problem is there. If it still doesn't work, it's most likely not the problem.
    OR you can add a GUI element that displays the "path" so you can check if it returns the right folder. Also make sure the file is actually there.
     
  5. DerDree

    DerDree

    Joined:
    Jan 16, 2012
    Posts:
    6
    Jep thanks :) just checked this and posted it in the Edit above.
    My problem now is how I can automatically let unity put the .txt-file in the data-folder of the build project.
     
  6. Morning

    Morning

    Joined:
    Feb 4, 2012
    Posts:
    1,141
    No right now unity does not support selective packing and instead it packs all files in the asset (that are used), the ones that are not are simply skipped. If you know C# you could possibly write your own tool though.
    Alternatively you could check if the file exists and if it doesn't populate it with data if there is not a lot of it. Like have one text file referenced that will be compiled into the game and then just copy the contents into a fresh file. This also makes it so you have a "default" backup so if the player messes something up they can just delete the file and a default one is made.
     
    Last edited: Apr 18, 2012
  7. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    You can use TextAsset and having your TextAsset saved in the Resources folder

    edit:
    Of course you could read this file on first install and save them in the data folder (i.e. if the user is required to edit them). But if you don't have to edit them, just keep them in the resource folder and access them via Resource.Load
     
    Last edited: Apr 18, 2012
  8. DerDree

    DerDree

    Joined:
    Jan 16, 2012
    Posts:
    6
    Okay thank you very much.

    At the moment its enough for me to create a folder with .txt-files manually with every build but I will look into other options later.
     
  9. zagahlo

    zagahlo

    Joined:
    Aug 5, 2012
    Posts:
    6
    Same problem here and I can't solve it :'(