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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[SOLVED]SetActive/Json not working in Build Mode

Discussion in 'Scripting' started by Guilhaume, Dec 7, 2017.

  1. Guilhaume

    Guilhaume

    Joined:
    Sep 14, 2016
    Posts:
    30
    Hello !
    I'm fairly new to programming, and i have an issue with a project.
    I have a button that launches a method that does a bunch of things and finally activates another GameObject.
    That GameObject is declared as public and then linked correctly in the inspector at Start().

    That method also activates another GameObject.
    In both cases it is through:
    TheGameObject.SetActive(true);
    Both GameObjects are linked correctly through the inspector.

    In editor mode, it works perfectly, both objects are shown when i click the button, but when i build the project, only one of them works..
    I've tried a tons of stuff and really can't understand what's the issue, i have also looked online and i can't find a similar case with a right answer.

    If anyone know what could be the issue here that'd be great :/
    cheers

    [SOLUTION]
    For thoses who have problem with loading json when they build their game.

    Application.DataPath, in unity editor, leads to the assets folder
    When the game is built, Application.DataPath leads to the root of your game, so you need to create a data folder in your build folder, and put your json files there.

    Ex:
    Create a Data folder and put your json file(s) in it:
    GameName/Data/MyJsonFile.json

    And when you load in your script:
    string myPath = "/Data/MyJsonFile.json";
    filePath = Application.DataPath + myPath;
     
    Last edited: Dec 8, 2017
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Bunny83 likes this.
  3. Guilhaume

    Guilhaume

    Joined:
    Sep 14, 2016
    Posts:
    30
    hi, thanks for your response,
    i'm not sure which document to find here, there is no indication concerning windows10.
    I only have an editor.log found through the console(open editor log).
    When i launch the build it doesn't crash. Can i access the logs through the build folder ?
     
  4. Guilhaume

    Guilhaume

    Joined:
    Sep 14, 2016
    Posts:
    30
    This is the editor log
     

    Attached Files:

  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Not talking about the editor log. I'm talking about the log your standalone build produces when it runs (the build you say is having the problem). That log may be reporting some null reference error not seen in the editor, or something along those lines.

    My guess would be the log is stored in Windows 10 in the same location as Windows 7.
    C:\Users\username\AppData\LocalLow\CompanyName\ProductName\output_log.txt

    Note that it is in a hidden folder. Username would be your username in Windows, and CompanyName\ProductName would be what you told Unity in the project settings.

    If you're still having trouble finding the log, you can force the log to whatever location you want with the -logFile command line argument when you launch your standalone build.

    https://docs.unity3d.com/Manual/CommandLineArguments.html
     
    Bunny83 likes this.
  6. Guilhaume

    Guilhaume

    Joined:
    Sep 14, 2016
    Posts:
    30
    I think it's this one, company name was "DefaultCompany" since i don't recall having set one.

    Indeed there is a NullReferenceException here !

    Gonna check this right now ;)
     

    Attached Files:

  7. Guilhaume

    Guilhaume

    Joined:
    Sep 14, 2016
    Posts:
    30
    Yup, i think know what's going on, i just don't know how to fix it.
    I have a Json file and it's not being loaded apparently..

    I don't know why, but now i have a console appearing in the bottom left of my screen when i launch my build.

    It says :
    NullReferenceException: Object reference not set to an instance of an object
    at CardsManager.PickRandomCard () [0x00002] in E:\Unity_Projects\Floating Islands\Assets\Scripts\Cards\CardsManager.cs:50
    at EventManager.GenerateEvents () [0x00013] in E:\Unity_Projects\Floating Islands\Assets\Scripts\EventManager.cs:80

    Here is the related code:


    [EventManager.cs:80]:
    private Card currentCard;
    ...
    public void GenerateEvents()//Launch the next event
    {
    calendar.NewWeek();//Updates the week/season/moon cycle

    //Chooses a random general Event
    currentCard = cardsManager.PickRandomCard();//This returns a random card

    ---------------------------------------------------------------------------------------------------------------------
    [CardsManager.cs:50]:
    //This returns a random card
    public Card PickRandomCard()
    {
    int currentId;
    do
    {
    currentId = Random.Range(0, cards.Length);//THIS IS THE LINE 50 (the problematic one)
    if (cards[currentId].enabled)
    {
    break;
    }
    } while (true);
    return cards[currentId];
    }

    ---------------------------------------------------------------------------------------------
    I have a Json file that has a
    Public class Card

    That's why the return type here is Card.
     
    Last edited: Dec 8, 2017
  8. Guilhaume

    Guilhaume

    Joined:
    Sep 14, 2016
    Posts:
    30
    I have narrowed it a little, seems it is the cards.Length that is the problem.
    It loads the json fine in the editor, but seems it doesn't when i build it..

    This is how i load the json :

    private string communEventsPath = "/Resources/Json/CommunEventsJson.json";

    public void LoadData()
    {
    string filePath = Application.dataPath + communEventsPath;

    if (File.Exists(filePath))
    {
    string dataAsJson = File.ReadAllText(filePath);
    dataAsJson = "{\"Items\":" + dataAsJson + "}";
    cards = JsonHelper.FromJson<Card>(dataAsJson);
    }
    }

    I have already tried with Resources.Load, doesn't change anything, and i couldn't managed to load it with TextAsset. but everything is correctly loaded in the editor anyway :/
     
    Last edited: Dec 8, 2017
  9. samra2494

    samra2494

    Joined:
    Nov 23, 2015
    Posts:
    21
    string myPath = "/Data/ItemsData.json";
    string filePath = Application.dataPath + myPath;
    json = File.ReadAllText(filePath);
    gameData = JsonUtility.FromJson<GameData>(json);
    print(gameData.Items.Count);

    not working at all in windows build :'( plz help
     
  10. Sebelulu

    Sebelulu

    Joined:
    Nov 24, 2012
    Posts:
    2
    If any of you are still having issues with this. Add the JSON file to your gameName_Data folder and it should work.
    Then you do not need to change the filepath either :)
     
  11. o_farzaliyev

    o_farzaliyev

    Joined:
    Dec 8, 2020
    Posts:
    2
    [SOLUTION]
    For thoses who have problem with loading json when they build their game.

    Application.DataPath, in unity editor, leads to the assets folder
    When the game is built, Application.DataPath leads to the root of your game, so you need to create a data folder in your build folder, and put your json files there.

    thanks for posting a solution as well. one question I have. Will this solution work also for published games, i.e. let's say somebody downloads the game to their device, will unity build include these json file as well? thanks in advance.
     
  12. RandomBinaries

    RandomBinaries

    Joined:
    Oct 2, 2017
    Posts:
    5

    Hi o_farzaliyev,

    How does one go about doing that? Would, after building the game for Windows, go to the folder that the game is built to & create a new folder there?