0
\$\begingroup\$

I have the following classes and json file, i thought id followed the documentation exactly but I am clearly wrong. Can anyone see why I get an error just saying : INVALID VALUE in my unity console:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

[Serializable]
public class DataReader
{
public float[] x;
public float[] y;
public float[] z;
}

GameController:

public class GameController : MonoBehaviour { public static GameController instance;

DataReader dataReader;



private string gameDataFileName = "level1.json";



private void Awake()
{
    dataReader = JsonUtility.FromJson<DataReader>(gameDataFileName);
    instance = this;
}

void Start () {


    // create level:
    print("DATA: " + dataReader.x[0]);
}


}

and the json is stored in StreamingAssets folder (I also tried a copy in Resources folder):

{
"x": [{1},{1},{1},{1}],
"y": [{1},{1},{1},{1}],
"z": [{1},{2},{3},{4}]
}
\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Remove the curly braces from your values.

{
"x": [1,1,1,1],
"y": [1,1,1,1],
"z": [1,2,3,4]
}

Though I'd also recommend for sanity sake to group by point and not by axis.

[Serializable]
public class Point
{
  public float x;
  public float y;
  public float z;
}

[Serializable]
public class DataReader
{
  public Point[] points;
}

{
  "points": [
    {
      "x": 1,
      "y": 1,
      "z": 1
    },
    {
      "x": 1,
      "y": 1,
      "z": 2
    },
    ...
  ]
}
\$\endgroup\$
3
  • \$\begingroup\$ thanks again Chris, you have solved my incorrect syntax inside the json and I now understand how they are to be completed. But unfortunately I still have the vague error message of : Invalud Value in Unity. \$\endgroup\$ Aug 15, 2017 at 17:38
  • 1
    \$\begingroup\$ try changing the numbers to 1.0 or 1.0f or "1.0f". Unity needs to know that the numeric constant is a float and not an integer. \$\endgroup\$
    – CLo
    Aug 15, 2017 at 17:41
  • \$\begingroup\$ just letting you know i fixed it finally. I needed to make a TextAsset tohold the json name string. Ive posted the full code as another answer for anyone who is confused the same as me in future \$\endgroup\$ Aug 15, 2017 at 20:18
1
\$\begingroup\$
public class GameController : MonoBehaviour {
public static GameController instance;

//public InterstitialAd interstitialAd;

TextAsset textAsset;


DataReader dataReader;

List<float> xList = new List<float>();

private void Awake()
{
    textAsset = Resources.Load<TextAsset>("level1");
    print("JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJSON = " + textAsset.text);
    dataReader = JsonUtility.FromJson<DataReader>(textAsset.text);

    instance = this;
}

void Start () {
    // this just requests the ad, doesnt show it:
    //interstitialAd = AdvertsInterface.instance.RequestInterstitial();

    for (int i = 0; i < dataReader.x.Length; i++)
    {

        xList.Add(dataReader.x[i]);
    }
    // create level:
    print("DATA: " + xList[0]);
}




void Update () {


}

}

This code fixed it. I was missing the TextAsset taht whats confused me the whole time.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .