Search Unity

Extremely Simple Linear Search Doesn't Work on Android & iOS

Discussion in 'Getting Started' started by shonsirsha, Jul 23, 2019.

  1. shonsirsha

    shonsirsha

    Joined:
    Jul 23, 2019
    Posts:
    1
    I am a total beginner on C# and even more on Unity. I have tried to look for many things on here but didn't find one that solved the problem. This seems like a super simple linear search but it didn't work on iOS & Android and really stressed me out.

    It works only on my Mac when I clicked the Play button thingy on top. I have no clue at all. Just FYI, the array length is 119.


    I have a JSON, the structure is as follows:
    ```
    Code (JavaScript):
    1. {
    2.     "Items": [
    3.         {
    4.             "name": "Hydrogen",
    5.             "symbol": "H",
    6.             "boil": 231
    7.         },
    8.         {
    9.             "name": "Sasdsad",
    10.             "symbol": "S",
    11.             "boil": 213
    12.         }
    13.     ]
    14. }
    ```
    and I have parsed it using a C# class,

    ```
    Code (CSharp):
    1. public class ChemElements {
    2.  
    3. public string name;
    4. public float boil;
    5. public string symbol;
    6.  
    7.  
    8. }
    ```

    and then on start() method I put it inside an array called x :


    ```
    Code (CSharp):
    1. ChemElements[] x = null;
    2.  
    3. void start(){
    4. path = Application.streamingAssetsPath + "/periodic.json";
    5. jsonstring = File.ReadAllText(path);
    6. x = JsonHelper.FromJson<ChemElements>(jsonstring);
    7. }
    ```

    JsonHelper is as follows, it's just a helper class to deserialize JSON array:

    ```
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public static class JsonHelper
    5. {
    6.     public static T[] FromJson<T>(string json)
    7.     {
    8.         Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
    9.         return wrapper.Items;
    10.     }
    11.  
    12.     public static string ToJson<T>(T[] array)
    13.     {
    14.         Wrapper<T> wrapper = new Wrapper<T>();
    15.         wrapper.Items = array;
    16.         return JsonUtility.ToJson(wrapper);
    17.     }
    18.  
    19.     public static string ToJson<T>(T[] array, bool prettyPrint)
    20.     {
    21.         Wrapper<T> wrapper = new Wrapper<T>();
    22.         wrapper.Items = array;
    23.         return JsonUtility.ToJson(wrapper, prettyPrint);
    24.     }
    25.  
    26.     [System.Serializable]
    27.     private class Wrapper<T>
    28.     {
    29.         public T[] Items;
    30.     }
    31. }
    ```

    What I'm trying to do is to search thru the array x of ChemElements[]
    by linear search (a very simple one) by matching a TextMeshPro text with the symbol property of x (array). If the symbol is found, I want to set a new value the alpha of ElPreview
    ```
    Code (CSharp):
    1. public void eleClicked(){
    2.         var eleBtn = GameObject.Find(EventSystem.current.currentSelectedGameObject.name);
    3.        for(int i = 0; i < x.Length; i++){
    4.            if(x[i].symbol == eleBtn.GetComponentInChildren<TMP_Text>().text){
    5.                    ElPreview.canvasRenderer.SetAlpha(1f);
    6.            }
    7.        }
    8.  
    9.      
    10.      
    11.    }
    ```

    eleClicked() is an onclick function. I've tried to use .equals() instead of == , but got 0 success.

    In the unity, as I told previously, it runs as expected. It changes the alpha value to 1, causing it to show (previously was 0).

    Whereas on iOS & Android it DIDN'T. The alpha value I assume is still 0, since it's stil transparent.

    I have tried to remove the linear search, and only change the alpha value and it works on iOS & Android, therefore I know the problem lies within the linear search code.

    Thank you very much!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It's hard to see how such a simple for-loop could go wrong. To get to the bottom of this, litter your code with Debug.Log statements, then pull the log and see what's happening. For example, on every iteration of the loop, log x.symbol, eleBtn.GetComponentInChildren<TMP_Text>().text, and whether they are equal.

    And while you're checking the log, be sure to check for unhandled exceptions too. It could be your code isn't running at all, or is bailing out early, because of one of those.