Search Unity

Problem with running on Android device

Discussion in 'Scripting' started by Aktik, May 23, 2020.

  1. Aktik

    Aktik

    Joined:
    Mar 28, 2020
    Posts:
    11
    Hey, I try to make a game which should run on a samsung tab A 2018 10.5 . I've already created most of it, but with my new Inventory, I came across with a problem which I can't solve. The Problem is that the App shutdown^^

    Is my Inventory Method too complex?

    The Inventory has:
    -1 Headline (Text) called by "Info"
    - 6 Buttons with Text (for gear)
    -2 Images (and Text) for the collection (gold)
    and 2 Buttons which lead in a sub inventory which contains potion and curses (irrelevant for the problem)

    Heres the Code that Opens the Inventory when the Button is pressed :

    Code (CSharp):
    1. public void OpenInventory()
    2.     {
    3.         if (ChangeSc.Blende.GetBool("begin")) //If Fading
    4.             return;
    5.         string scene = SceneManager.GetActiveScene().name;
    6.         if (scene == "Battle")
    7.         AttackHandling.click.Play();
    8.         AttackHandling.attackid = 0;
    9.         Unit.Charaktere Player;
    10.        
    11.         if (!Inventory.activeSelf)
    12.         {
    13.             if (scene == "Battle")
    14.                 Player= WhosTurn.AmZug;
    15.             else
    16.                 Player=Who.AmZug;
    17.             int counter = 0;
    18.             Unit.Ausrüstung Teil;
    19.             Inventory.SetActive(true);
    20.             foreach (Text item in Inventory.transform.GetComponentsInChildren<Text>().Where(x=>x.name.Contains("Slot")))
    21.             {
    22.                 Teil = Player.GetSlot(counter);
    23.                 item.text = "<color=" + colors[Teil.GetTüp()] + "> " + Teil.GetItemName() + "</color>";
    24.                 if (Teil.GetTüp() < 4 && Teil.GetTüp() != 0)
    25.                 {
    26.                     item.text += " Haltbarkeit " + Teil.GetDura();
    27.                     if (Teil.isActive)
    28.                         item.text += "  <color=yellow>active</color>";
    29.                 }
    30.                 if (scene == "Battle")
    31.                 {
    32.                     if (Teil.GetItemName() != "Leer" && counter <= 3 || (counter == 5 && (Teil.GetItemName() == "Junteras Münze" || Teil.GetItemName() == "Joker")))
    33.                         item.gameObject.GetComponent<Button>().interactable = true;
    34.                     else if (counter != 4)
    35.                         item.gameObject.GetComponent<Button>().interactable = false;
    36.                 }
    37.                 else if (scene == "HauptBild")
    38.                 {
    39.                     if (counter == 5 && (Teil.GetItemName() == "Trickwürfel"))
    40.                         item.gameObject.GetComponent<Button>().interactable = true;
    41.                     else if (counter != 4)
    42.                         item.gameObject.GetComponent<Button>().interactable = false;
    43.                 }
    44.                 item.gameObject.name = Teil.GetItemName();
    45.                 counter++;
    46.             }
    47.             Info.SetActive(true);
    48.             Inventory.transform.GetChild(7).GetChild(0).GetComponent<Text>().text = Who.AmZug.GetGold() + "";
    49.             Inventory.transform.GetChild(8).GetChild(0).GetComponent<Text>().text = Who.AmZug.essence + "";
    50.         }
    51.         else
    52.         CloseInventory();
    53.     }
    Basically each Character has 6 normal slots. While the first 4 can always be interactable, the fifth is never interactable and the sixth only if it's a special item. Each slot is an object of the class "Ausrüstung" which contains Itemname, Durability, activeStatus and two ints for stats.

    Calling this function for the first time, takes an fair amount of time on my tablet and on my smartphone it doesn't even load (it shutdown instant).

    What this should do:
    -Check every Slot. If it's in the first four slots (and not empty) also show durability (with GetDura()) and if the item is equipped, the text is extended by "active". Each Slot also has two Eventtrigger, by holding down, the image of the item is displayed (because I can't make an hover event on mobile devices).

    Is it just too much? I know that unity checks for all Eventrigger if the Eventtrigger component is attached. Is this the problem? With my PC everything works perfectly fine :/
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I suggest attaching
    adb logcat
    via USB and seeing if there are any error messages being printed to the Android console log when your code runs, such as exceptions or otherwise. There are lots of tutorials on setting the
    adb
    tool up and it comes already in the Android SDK packages.
     
  3. Aktik

    Aktik

    Joined:
    Mar 28, 2020
    Posts:
    11
    I tried this but nothing useful shows up. My debug log calls show up. But not a single error when it shuts down :/
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Is it possible that some of the objects involved, such as the inventory tiles, have extremely large textures? If so, you could notice this on the PC by attaching the profiler and seeing how much memory usage increases when you run this.

    A simple quick test for this is to select all the involved graphics and change their max size to something small like 256x256, even though it will NOT look good, and see if that fixes it. Or you can attach the profiler and attempt to determine more specifically, but if it loads up an excessive amount of memory in a single frame, then crashes, the profiler will not get a chance to report that back.

    I only see one loop above, the main foreach loop... you could put an extra check in there that forces a break when counter reaches a certain point. First you could try exiting when the counter ==1, and if that works, then 2, etc.
     
  5. Aktik

    Aktik

    Joined:
    Mar 28, 2020
    Posts:
    11
    Ok I tested a bit. Even without the entire foreach loop it still occurs. I just reduced the texturesize of everything big in the scene (which is just the Background and the button texture) and still...

    I've zero clue how exactly I should work with the profiler. From what I see here, there is not much interesting in the memory part except all 200 frames 10kb gc gets alloced (I'm sorry, I've no idea if this is normal)

    The Inventory itself has only 6 textures:

    The Button
    Background
    2 for collection (50x50)
    And two arrows which max Size I've set to 256
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Were you able to make functioning Android builds (of any kind) for this device all the way up until this inventory system was added?
     
  7. Aktik

    Aktik

    Joined:
    Mar 28, 2020
    Posts:
    11
    Weil not completly, but it was often caused by other errors which I could fix. Right now, if I doesn't click the inventory, everything seems to work. Could it bei because the inventory maybe starts inactive? In another scene I can call all inventory items as just images, which I can add buttons to without Problems.

    Edit: ok checked it, the inventory starts active, but deactivate itself in the Start function.

    Edit 2: ok I guess it was just too much. I created a gameobject which holds now all 6 slots (instead of just 6 gameobject which are just children of the Background Image). This seems to speed up this a lot. Now I can call the inventory almost instanly and the game hasn't shutdown (yet). Still thanks for the Help :)
     
    Last edited: May 24, 2020
    Kurt-Dekker likes this.