S
S
Sergey Bezrukov2019-01-30 14:23:05
C++ / C#
Sergey Bezrukov, 2019-01-30 14:23:05

Object reference not set to an instance of an Object Why is unity throwing this error?

public void Awake()
{
sv = JsonUtility.FromJson<Save>(PlayerPrefs.GetString("SV"));
Debug.Log(sv.achieveStatus.Length);
}
public void OnApplicationQuit()
{
sv.achieveStatus = new byte[11, 3];
PlayerPrefs.SetString("SV", JsonUtility.ToJson(sv));
PlayerPrefs.Save();
}

In the second case, in the OnApplicationQuit function , 33 is printed to the console. In the first case, in the Awake function , it gives an error Object reference not set to an instance of an Object . Sv is a class whose variables are set to values ​​that need to be saved. All the same with a one-dimensional array works fine. What could be the reason?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2019-01-30
@Hrederik

did you forget to initialize the array somewhere?
if other variables and fields from sv are available..
sv.achieveStatus = new int[,]
or not int. but you get the gist.
otherwise you will get NUllRef)) you can’t take the length from ANYTHING))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question