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

Saving my array for inventory in-between scenes works fine in the editor but not in the build.

Discussion in 'Scripting' started by Datchcole, Dec 9, 2021.

  1. Datchcole

    Datchcole

    Joined:
    Jun 8, 2019
    Posts:
    6
    This has really stumped me. I have an array of strings to keep track of inventory. I use Easy Save 3 to save between scenes and it seems to be working properly as on my inventory UI it correctly lists picked up items. However, the meshes are supposed to disable if the item is picked up and doors are supposed to be unlocked if the item is in the player's inventory. This all works together perfectly when I play it in the editor, but when I make a build item objects reappear and doors remained locked unless I re-pickup the item if I switch scenes.

    Any ideas on what might cause this? I feel like the code should be just fine, since it work fine in the editor, and its more of me not understanding something about how builds operate differently?

    Here's my main inventory script:


    Code (CSharp):
    1.     public string itemName;
    2.     public float itemDistance;
    3.     public GameObject inventoryUI;
    4.     public GameObject inventoryImage;
    5.     public bool inventoryOn;
    6.     public string localItem;
    7.     public GameObject inventoryWord;
    8.    
    9.     public List<string> inventoryArray;
    10.     public List<string> currentArray;
    11.     public bool inventoryUpdated;                    //keep track if inventory needs to be updated or not
    12.     public bool checker;
    13.     bool inventoryOverwritten;
    14.  
    15.  
    16. void Start(){
    17.             inventoryOn = false;
    18.             inventoryImage.SetActive(false);
    19.             inventoryUI.SetActive(false);
    20.             inventoryUpdated = false;
    21.             inventoryWord.SetActive(false);
    22.             inventoryUpdated = true;
    23.    
    24. }
    25.  
    26. void Update(){
    27.        
    28.  
    29.         if(inventoryUpdated == true ){
    30.             inventoryUI.GetComponent<Text>().text = "";
    31.            
    32.             foreach(var x in inventoryArray){
    33.  
    34.                     inventoryUI.GetComponent<Text>().text +=x;
    35.  
    36.             }
    37.             inventoryUpdated = false;
    38.            
    39.         }
    40.        
    41.  
    42.  
    43.         if(Input.GetKeyDown(KeyCode.I) && inventoryOn == false){
    44.             Debug.Log("Pressed I Inventory ON");
    45.  
    46.             inventoryUI.SetActive(true);
    47.             inventoryImage.SetActive(true);
    48.             inventoryWord.SetActive(true);
    49.             inventoryOn = true;
    50.            
    51.             Debug.Log("Inventory Current: " + inventoryArray);
    52.  
    53.         }else if(Input.GetKeyDown(KeyCode.I) && inventoryOn == true){
    54.             Debug.Log("Pressed I Inventory OFF");
    55.             inventoryUI.SetActive(false);
    56.             inventoryImage.SetActive(false);
    57.             inventoryWord.SetActive(false);
    58.             inventoryOn = false;
    59.         }
    60.  
    61.        
    62.  
    63.     }
    64.  
    65. public void UpdateInventory(string item){
    66.    
    67.     inventoryArray.Add(item);  
    68.     Debug.Log("Array after Pass to Main: ");
    69.    
    70.     foreach(var x in inventoryArray){
    71.         Debug.Log("||||||||||     Inventory from Update Inventory Checker: |||||||| " + x+ "  ");
    72.     }
    73.     inventoryUpdated = true;
    74.  
    75. }
    76.  
    77. public void overWriteInventory(List<string> newInventory){
    78.     inventoryArray = newInventory;
    79.     foreach(var x in inventoryArray){
    80.         UpdateInventory(x);
    81.     }
    82.    
    83.    
    84.    
    85. }
    86.  
    87. public bool checkInventory(string name){
    88.    
    89.     foreach(var x in inventoryArray){
    90.         if (string.Equals(x, name)){
    91.             checker = true;
    92.         }else{
    93.             checker = false;
    94.         }
    95.     }
    96.    
    97.     return checker;
    98.    
    99.    
    100.    
    101. }

    Item checks for a save variable's status to check if it should deactivate an already picked up item on scene change:

    Code (csharp):
    1.  
    2. void Update(){
    3.              
    4.  
    5.         if(deactivated == true){
    6.             item.SetActive(false);
    7.         }
    8.        
    9.  
    10. }
    11.  
    Doors will call checkInventory() in my inventory script to see if a matching string is in the inventory and if so will become unlocked.

    If any more information is needed, please let me know! And thank you for any help!
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    What Build-Platform are we talking about? Is Easy Save trying to save a file on a mobile device which it doesn't have the rights to?
     
    seejayjames and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    If nothing loads / saves this is a great place to start: make sure the file read/write is succeeding. At least START with this, because if the data isn't coming back, well, nothing in your code can fix that!

    But if you say some of it works while some of it does not, and that it is platform dependent, one place to look is code execution order. One way to truth this out is to go nuts with Debug.Log() statements to try and find out if perhaps your code has an unknown dependency on execution order that you "just get away with" in the editor.

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Try a stripped-down version of saving with PlayerPrefs and see if that works. If so, it's an issue with EasySave behaving badly...
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    Or you could just JSON out the list of strings normally (using Newtonsoft JSON) rather than cram strings into PlayerPrefs.
     
  6. Datchcole

    Datchcole

    Joined:
    Jun 8, 2019
    Posts:
    6
    I'll use some Debug.Log to check and make sure scripts are running in the build properly and go from there. It's strange because the UI for the inventory is saved properly, and everything else saves and loads properly in the game (dialogue variables, trigger states, player location, last scene number, etc.). Only in the build version of the game am I having an issue with only items and doors seemingly not detecting the string name in the inventory array even though the UI and Debug.Log for the array contents show the proper strings are stored there.(The issue happens after I switch scenes and return and have reloaded the saved inventory string back into the player's inventory)

    Do builds execute much differently from when a game is ran in the editor? Like it could mess up the order of execution possibly or something like that which could cause issues?