Search Unity

Game not working correctly after building it for android.

Discussion in 'UGUI & TextMesh Pro' started by spinifer_x, Feb 5, 2017.

  1. spinifer_x

    spinifer_x

    Joined:
    Dec 6, 2016
    Posts:
    32
    Hey,
    I have completed developing my 2D game for android and everything was working fine in my unity editor. But when I build it for android and installed it on my android devices the following problems came into play :

    1. For some android devices ,only half of the screen is showing,for other half my game object is invisible.

    2. In my unity editor,everything is running fine but for android devices buttons are not working correctly . Sometimes they work and sometimes they don't.

    3.Also levels are loading slowly but when i am testing it using unity remote from my computer everything seems to be fine.

    Can anyone please tell me how to fix this issue ? Is there is any way or build settings that i can use to get rid of this problem?
     
  2. zafar_iqbal

    zafar_iqbal

    Joined:
    Apr 20, 2017
    Posts:
    1
    Watch charger games videos on youtube that has your answers
     
  3. zki

    zki

    Joined:
    Sep 23, 2017
    Posts:
    1
    At least post the link of solution.
     
    Ch267, AbdulrahmanaAbs and rahulk1991 like this.
  4. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Hey there,

    Not a good idea to test the game at the very end on the target device. I guess you thought you tested it through unity remote, but unity remote streams the image that is calculated in the editor to your device and pushes back the input you are giving on the phone back to the editor, so it is not running on the device, but in the editor. This should answer question three of yours.

    With the other two questions, you are providing way too little information in order to really tell where the problem lies.
     
  5. marckaldousmatawaran06

    marckaldousmatawaran06

    Joined:
    Mar 20, 2018
    Posts:
    10
    I have the same problem too sometimes my game ignore the rigid body 2d. Is there is any way or build settings that I can use to get rid of this problem?
     
  6. marckaldousmatawaran06

    marckaldousmatawaran06

    Joined:
    Mar 20, 2018
    Posts:
    10
    I have the same problem too sometimes my game ignore the rigid body 2d. Is there is any way or build settings that I can use to get rid of this problem?
     
  7. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    I don't really see how that is related to the question, but check your rigidbody settings. If they are clipping through other objects, it might be because they are too fast to trigger collisions in discrete mode. I would recommend setting the following settings:
    upload_2018-8-10_10-44-14.png
    Collision Detection: Continuous
    Interpolate: Interpolate
     
  8. marckaldousmatawaran06

    marckaldousmatawaran06

    Joined:
    Mar 20, 2018
    Posts:
    10
    thanks but it doesn't fix my problem :(example A BALL when I try it in unity editor when it's moving its pretty damn fine but when I install it in android the ball moving to slow :< I hope you can help me with this thanks for concern :)
     
  9. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Hm, that sounds strange, I didn't experience anything like that before. I would recommend creating a new thread in one of the fitting subforums (I would recommend Physics):
    Physics https://forum.unity.com/forums/physics.78/
    Android: https://forum.unity.com/forums/android.30/
    2D: https://forum.unity.com/forums/2d.53/

    Try to describe the problem more precise, so others can get a better idea of where the problem is coming from. And try to isolate the problem. For example try to create a scene with a ball that has just the physics 2d rigidbody attached and falls down. Is the behavior different or the same with this simple setup?
     
  10. marckaldousmatawaran06

    marckaldousmatawaran06

    Joined:
    Mar 20, 2018
    Posts:
    10
    example A BALL when I try it in unity editor when it's moving its pretty damn fine but when I install it in android the ball moving to slow :<. My game is a pet simulating and other games like basketball and geometry dash when i test it in unity editor all was just fine but when i install it on Android in basketball my pet was moving to slow and the ball and in like a geometry dash game. his jump should be a consistent force but when in android when i jump his jump sometimes it's to much force but sometimes it's less than the to my codes :<
     
  11. justbb

    justbb

    Joined:
    Apr 24, 2019
    Posts:
    6
    Hey hi, I think my problem is with saving and loading data which works fine for windows build and inside editor but not for android.
    Problem: I have two toggle buttons which change characters and remembers it across scenes and sessions. I saved the character index using PlayerPrefs, at the beginning of the game the character with index 0 should be visible as that is the first char in the line and rest are set to deactivate state. when I opened the game in android mobile all the characters are visible and there is a button that increases the coin count and it is stored in a binary file, but when pressed the coins are not increasing, so what i know is everything related to saving is not working. can you help me with this?
     
  12. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    I can assure you, that PlayerPrefs work as expected on android and don't have any different behavior, compared to PlayerPrefs on a Windows Build (At least, up to 2019.3, didn't work with 2020 alpha).

    My first instinct would be: The script execution order is not that reliable, so it might be that Awake from object A is called before Awake on object B on windows, but not on android. If you're relying on a specific execution, this might be something you want to refactor.

    Other thoughts: Are they in a toggle group? If so, you might listen to onValueChanged while you're still in the setup process of changing your values, which might overwrite your playre Prefs in an unexpected way. Try to do a simple build which just shows your PlayerPref, and an option to increase the value, that might help you pinpoint the problem.

    If you post your toggle script (and all connected scripts), I might be able to help you better.
     
    justbb likes this.
  13. justbb

    justbb

    Joined:
    Apr 24, 2019
    Posts:
    6
    Code (CSharp):
    1. void Start()
    2.     {
    3.         index = PlayerPrefs.GetInt("Selected"); // seting index to remember
    4.         // GameObjects assaigning into an array
    5.         gameObjects = new GameObject[transform.childCount];
    6.         for (int i = 0; i < transform.childCount; i++)
    7.             gameObjects = transform.GetChild(i).gameObject;
    8.         //Deactivate all gameobjects
    9.         foreach (GameObject go in gameObjects)   // Error1 (not deactivated)
    10.             go.SetActive(false);
    11.         //Activate first Gameobject in the array Index(0)
    12.         if (gameObjects[index])
    13.         {
    14.             gameObjects[index].SetActive(true);
    15.         }
    16.     }
    17.     public void ToggleLeft()
    18.     {
    19.         // Deactivate current object
    20.         gameObjects[index].SetActive(false);
    21.         // Change Object
    22.         index--;
    23.         if (index < 0)
    24.         {
    25.             index = gameObjects.Length - 1;
    26.         }
    27.         //Activating new Object
    28.         gameObjects[index].SetActive(true);
    29.     }
    30.                public void ToggleRight()
    31.     {
    32.         //Deactivate
    33.         gameObjects[index].SetActive(false);
    34.         //Change object
    35.         index++;
    36.         if (index == gameObjects.Length)
    37.         {
    38.             index = 0;
    39.         }
    40.         // Activate new object
    41.         gameObjects[index].SetActive(true);
    42.     }
    43.                public void ConformChar() // Conforms char and Loads scene;
    44.     {
    45.         PlayerPrefs.SetInt("Selected", index);
    46.         SceneManager.LoadScene(1); //Loading Game scene
    47.    
    48.     }
    49. [code=CSharp]void Start()
    50.     {
    51.         index = PlayerPrefs.GetInt("Selected"); // seting index to remember
    52.         // GameObjects assaigning into an array
    53.         gameObjects = new GameObject[transform.childCount];
    54.         for (int i = 0; i < transform.childCount; i++)
    55.             gameObjects[i] = transform.GetChild(i).gameObject;
    56.         //Deactivate all gameobjects
    57.         foreach (GameObject go in gameObjects)   // Error1 (not deactivated)
    58.             go.SetActive(false);
    59.         //Activate first Gameobject in the array Index(0)
    60.         if (gameObjects[index])
    61.         {
    62.             gameObjects[index].SetActive(true);
    63.         }
    64.     }
    65.     public void ToggleLeft()
    66.     {
    67.         // Deactivate current object
    68.         gameObjects[index].SetActive(false);
    69.         // Change Object
    70.         index--;
    71.         if (index < 0)
    72.         {
    73.             index = gameObjects.Length - 1;
    74.         }
    75.         //Activating new Object
    76.         gameObjects[index].SetActive(true);
    77.     }
    78.                public void ToggleRight()
    79.     {
    80.         //Deactivate
    81.         gameObjects[index].SetActive(false);
    82.         //Change object
    83.         index++;
    84.         if (index == gameObjects.Length)
    85.         {
    86.             index = 0;
    87.         }
    88.         // Activate new object
    89.         gameObjects[index].SetActive(true);
    90.     }
    91.                public void ConformChar() // Conforms char and Loads scene;
    92.     {
    93.         PlayerPrefs.SetInt("Selected", index);
    94.         SceneManager.LoadScene(1); //Loading Game scene
    95.    
    96.     }
    The same script is used across both shop and game scenes, so char that is selected is what appears in the GamePlay scene. But the Gameobject of index zero is the only object active in the game scene.but is shop all objects are active.
    /////////////////////////////////////////////////////////////////////////////
    For saving points and high score, I followed this tutorial from Brackeys I think you must have watched this video I followed it as it is and got no problem in the editor and windows build except android.
    savedata() is called in update()
    Loaddata() is called in Awake()
    all this code lies in one script
    I'm a beginner and I really didn't get your point there. But this is my code.
     
  14. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Hi @justbb,

    Very good, now I can help you! I took a quick look at the script, and I think I found the problem in line 7:
    Code (CSharp):
    1. for (int i = 0; i < transform.childCount; i++)
    2.     gameObjects = transform.GetChild(i).gameObject;
    This should be like in your second script:
    Code (CSharp):
    1. for (int i = 0; i < transform.childCount; i++)
    2.     gameObjects[i] = transform.GetChild(i).gameObject;
    I hope that you're up for some tips:
    • The two script do basically the same, so you actually need only once script for the ui and the character models. Having less scripts in general makes your code more maintainable, so always try not to repeat yourself.
    • Try to make sure that your naming is correct. ConformChar should probably be ConfirmChar or even better ConfirmCharacterSelection. Refactoring those methods is of course possible, but might lead to unwanted results, when using those methods in UnityEvents in the scene.
    • When adding comments to Methods, I highly recommend using summary tags instead of plain comments. Those will show up in IDEs so you will get an easy way to quickly docment your code.

    I refactored the code, to give you an example of how it might be better to maintain. This is just as a reference and I hope it helps you to improve :)

    Code (CSharp):
    1. public class CharacterSelectionController : MonoBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         index = PlayerPrefs.GetInt("Selected"); // seting index to remember
    6.      
    7.         // GameObjects assigning into an array
    8.         gameObjects = new GameObject[transform.childCount];
    9.         for (int i = 0; i < transform.childCount; i++)
    10.         {
    11.             var go = transform.GetChild(i).gameObject;
    12.             gameObject[i] = go;
    13.             go.SetActive(false);
    14.         }
    15.      
    16.         //Activate first Gameobject in the array Index(0)
    17.         if (gameObjects[index])
    18.         {
    19.             gameObjects[index].SetActive(true);
    20.         }
    21.     }
    22.  
    23.     public void ToggleLeft()
    24.     {
    25.         ChangeIndexRelative(-1);
    26.     }
    27.  
    28.     public void ToggleRight()
    29.     {
    30.         ChangeIndexRelative(1);
    31.     }
    32.  
    33.     private void ChangeIndexRelative(int relativeChange)
    34.     {
    35.         // Deactivate current object
    36.         gameObjects[index].SetActive(false);
    37.         // Change Object
    38.         index = (index + gameObjects.Length + relativeChange) % gameObjects.Length;
    39.  
    40.         //Activating new Object
    41.         gameObjects[index].SetActive(true);
    42.     }
    43.  
    44.     /// <summary>Confirms character and Loads scene</summary>
    45.     public void ConfirmCharaterSelection()
    46.     {
    47.         PlayerPrefs.SetInt("Selected", index);
    48.         SceneManager.LoadScene(1); //Loading Game scene
    49.     }
    50. }
    Merry Christmas
    Johannes
     
  15. justbb

    justbb

    Joined:
    Apr 24, 2019
    Posts:
    6
    Sorry for the late reply, just thought not to ruin your Christmas. Don't know why unity works weird sometimes. The change of code you suggested me is what I was already using, I don't know how that got pasted that way. When I checked the code it was alright. If the line was
    Code (CSharp):
    1. gameObjects = transform.GetChild(i).gameObject;
    it would've been an error right in the code editor. My game worked fine inside the editor but not on my mobile. I've used your tips in writing clean code and used Right and Left toggle functions the way you suggested me. It didn't change anything but when I switched back to my code again, it started behaving just like the way it behaved on my mobile, I was about to quit Game dev :mad:
    But at last, I followed your first advice and made a new project and started building it again, first I've tested the character selection feature, worked fine :), and then tested scores and coins worked fine too :). I've decided to stick with Player Prefs not binary files. I hope everything will go fine. Thanks for your time bro. I'll keep asking more and more in the future.
     
  16. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Congrats for your endurance. Seems like the bug was hidden somewhere else. Sticking to simple player prefs is a good idea :)