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.
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:
{
"Items": [
{
"name": "Hydrogen",
"symbol": "H",
"boil": 231
},
{
"name": "Sasdsad",
"symbol": "S",
"boil": 213
}
]
}
and I have parsed it using a C# class,
public class ChemElements {
public string name;
public float boil;
public string symbol;
}
and then on start() method I put it inside an array called x :
ChemElements[] x = null;
void start(){
path = Application.streamingAssetsPath + "/periodic.json";
jsonstring = File.ReadAllText(path);
x = JsonHelper.FromJson<ChemElements>(jsonstring);
}
JsonHelper is as follows, it's just a helper class to deserialize JSON array:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class JsonHelper
{
public static T[] FromJson<T>(string json)
{
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
return wrapper.Items;
}
public static string ToJson<T>(T[] array)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper);
}
public static string ToJson<T>(T[] array, bool prettyPrint)
{
Wrapper<T> wrapper = new Wrapper<T>();
wrapper.Items = array;
return JsonUtility.ToJson(wrapper, prettyPrint);
}
[System.Serializable]
private class Wrapper<T>
{
public T[] Items;
}
}
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
public void eleClicked(){
var eleBtn = GameObject.Find(EventSystem.current.currentSelectedGameObject.name);
for(int i = 0; i < x.Length; i++){
if(x[i].symbol == eleBtn.GetComponentInChildren<TMP_Text>().text){
ElPreview.canvasRenderer.SetAlpha(1f);
}
}
}
eleClicked() is an onclick function.
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!