# Instantiate a sprite using json data value (C# Unity)

Here is my code :

BetBoard_Test.cs

 //Scoreboard
[SerializeField] protected GameObject prefab_big_road = null;

string jsonString = "[1, 1, 1, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]"; //sample data

private void Start()
{

ExampleClass dataParser = new ExampleClass();
dataParser.dataToParse = jsonString;

//Convert to Json
string exampleClassToJson = JsonUtility.ToJson(dataParser);
Debug.Log(exampleClassToJson);

ExampleClass obj = JsonUtility.FromJson<ExampleClass>(exampleClassToJson);
//Loop over it
for (int i = 1; i < obj.dataToParse.Length - 1; i += 3)
{
char indivisualChar = obj.dataToParse[i];

Debug.Log(indivisualChar);
}

WinLog();
}

{

yield return new WaitForEndOfFrame();

for (int i = 0; i < rh.Const._HISTORY_COUNT_ * rh.Const._HISTORY_HEIGHT_; i++)
{

int x = i % rh.Const._HISTORY_COUNT_;
int y = i / rh.Const._HISTORY_COUNT_;
float xl = 9.0f;
float yl = -8.0f;

GameObject o = Instantiate(prefab_big_road) as GameObject;
o.transform.localScale = Vector3.one; //(1,1,1)

o.transform.localPosition = new Vector3(x * xl, y * yl, 0f);
o.GetComponent<UISprite>().spriteName = array_big_road[i] == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";

NGUITools.SetActive(o, true);
yield return new WaitForEndOfFrame();
}
yield break;
}

void DeleteChildrens(Transform t)
{
NGUITools.DestroyChildren(t);
}

public void WinLog()
{
}
}

[Serializable]
public class ExampleClass
{
public string dataToParse;
}


ConstantValue.cs

public const int _HISTORY_COUNT_ = 70;
public const int _HISTORY_HEIGHT_ = 6;


PlayInfo.cs

public int[] _BIG_ROAD_ = new int[Const._HISTORY_COUNT_ * Const._HISTORY_HEIGHT_ ];


What i am trying to achieve here is that see image

my jsonString="[1, 1, 2, 2, 1, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1]"; that is converted into json format needs to do it something like this for example

1 = blue circle
2 = red circle


just like that in the picture every value on my json data needs to be instantiated with the sprite that is equivalent to 1 and 2 which is i have this condition o.GetComponent<UISprite>().spriteName = array_big_road[i] == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";

PS: I am very sorry if i couldn't explain it very well because english is not my native so i provided a picture. I am very sorry.

I did it like this but the problem is that it's not getting what i want all the red which is 2 is the only appearing on the board

 for (int i = 1; i < obj.dataToParse.Length - 1; i += 3)
{
char indivisualChar = obj.dataToParse[i];
int j = 0;

if(j < rh.Const._HISTORY_COUNT_ * rh.Const._HISTORY_HEIGHT_)
{
//lets increment it
j++;
//instantiate the sprite
GameObject o = Instantiate(prefab_big_road) as GameObject;
o.transform.localScale = Vector3.one; //(1,1,1)
int x = j % rh.Const._HISTORY_COUNT_;
int y = j / rh.Const._HISTORY_COUNT_;
float xl = 9.0f;
float yl = -8.0f;
o.transform.localPosition = new Vector3(x * xl, y * yl, 0f);
//o.GetComponent<UISprite>().spriteName = indivisualChar == 1 ? "layout_player_bigline-01" : "layout_banker_bigline-01";
if (indivisualChar == 1)
{
o.GetComponent<UISprite>().spriteName = "layout_player_bigline-01";
NGUITools.SetActive(o, true);
}
else
{
o.GetComponent<UISprite>().spriteName = "layout_banker_bigline-01";
NGUITools.SetActive(o, true);
}
}
//Debug.Log(indivisualChar);
}


All of the sprites are in one place and the second problem of that is all the prefab that is cloned is always red (2) which is 1 is for blue and 2 is for red.
o.GetComponent<UISprite>().spriteName = array_big_road[i] == '1' ? "layout_player_bigline-01" : "layout_banker_bigline-01";