Search Unity

Can not set scales on android build

Discussion in 'Scripting' started by Gonzasmpl, Jul 25, 2019.

  1. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    Hi. As I said, I test that everything works alright before building and indeed it does, but finally when i build it and test it on android, nothing get scaled.

    I'm for sure missing something but I really dont know where. I dont know if it has to do with Random.Range, the foreach loop or if it has to do with childs or what but, on android, it doesn't seem to be working. The piece of code is the following:
    Code (CSharp):
    1. public class LevelManager : MonoBehaviour
    2. {
    3.     private float[] tilesScales = {0.3f, 0.4f, 0.5f, 0.6f, 0.7f, 0.8f, 0.9f, 1f};
    4.  
    5.     private void Awake()
    6.     {
    7.         SetScales();
    8.     }
    9.  
    10.     void SetScales()
    11.     {
    12.         foreach (Transform child in transform)
    13.         {
    14.             if (child.transform.localPosition.y == 0)
    15.             {
    16.                 int randomScale = Random.Range(0, tilesScales.Length);
    17.                 child.transform.localScale = new Vector3(1, 1, tilesScales[randomScale]);
    18.             }
    19.         }
    20.     }
    21. }
    Then, just to try out, I made it so when I tap the screen it calls SetScales() again, but it obviously doesn't work either.

    If you know what should be causing the trouble or have any advice related to mobile devices I would be glad to hear it. Thanks!
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    So, localPosition.y is a float and checking equality for floats is not a good idea. I would try using https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html instead

    If you use logcat or a runtime console and debug your childs localPosition.y before your if check, it's possible it's actually .00000001 or something like that, which means they will never scale.
     
  3. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    You are right, I had a similar problem before and it could totally be the thing here. Later I will try to fix it up. Thanks!
     
  4. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    Unfortunately, That was not the problem here. I try using
    if (Mathf.Approximately(child.transform.localPosition.y, 0))

    and also
    if (Mathf.RoundToInt(child.transform.localPosition.y) == 0)

    but neither way do the work on android, despite working flawlessly inside unity editor.
    Any suggest?
     
  5. Gonzasmpl

    Gonzasmpl

    Joined:
    Jul 30, 2018
    Posts:
    55
    Through debugging I found out that (on Android) the void:
    Code (CSharp):
    1. void SetScales()
    2.     {
    3.         foreach (Transform child in transform)
    4.         {
    5.             if (Mathf.Approximately(child.transform.localPosition.y, 0))
    6.             {
    7.                 int randomScale = Random.Range(0, tilesScales.Length);
    8.                 child.transform.localScale = new Vector3(1, 1, tilesScales[randomScale]);
    9.             }
    10.         }
    11.     }
    Gets called properly and also the random number gets a correct value who throw the proper number from the array.
    The problem is that
    child.transform.localScale = new Vector3(1, 1, tilesScales[randomScale]);

    is not working, I also try to pass the floats manually but still nothing happens.