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\$
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\$
  • \$\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\$ – Big T Larrity Aug 15 '17 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\$ – Chris Aug 15 '17 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\$ – Big T Larrity Aug 15 '17 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\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.