D
D
Danil Shitov2020-09-22 15:26:26
JSON
Danil Shitov, 2020-09-22 15:26:26

From JSON to object does not work, what should I do?

The code doesn't work :((
There is a class

public class Player : MonoBehaviour
{
    public int[] levels = new int[3] {1,0,0};
}

the code is something like this:
Player player = new Player();
        string str = JsonUtility.ToJson(player);
        player = JsonUtility.FromJson<Player>(str);

Throws an error: ArgumentException: Cannot deserialize JSON to new instances of type 'Player.'
UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at <1386288601af43018501cce2912f52f4>: 0
)
)
What is the problem? help you need to convert an object from JSON.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-09-22
@daniilshitov

[Serializable]
public class Player {}

Without any MonoBehaviour, otherwise your json will weigh a kilogram.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question