简体   繁体   中英

Do I need to create object for subclass elements of a main class in c# unity3d?

I am sorry if this question is being asked because i missed something incredibly basic.

I have a classes:

    [Serializable]
    public class ResponseObject
    {
        public string id;
        public List<PlayerDetails> player_details ;
    }
    [Serializable]
    public class PlayerDetails
    {
        public int name;
        public int image ;
    }

And in other script have below code(

private ResponseObject resObject = new ResponseObject();
resObject = JsonUtility.FromJson<ResponseObject>(m_apiCallResponse);

Now when I try to access variables inside PlayerDetials class as below I am getting an

Debug.Log(resObject.player_details[0].name);

Error : Object reference not set an instance of an object at the Debug line

And I have data at 0th index of the List which I get from making API call, still I am getting the above error.

But above code works if I declare object as

private ResponseObject resObject = new ResponseObject();
resObject .player_details = new List<PlayerDetails>(); 
resObject = JsonUtility.FromJson<ResponseObject>(m_apiCallResponse);

Am I missing something, Do I need to compulsory declare in above manner?

And do I need to place [Serializable] on every class that I declare to make JsonUtility work, because its not working if I miss Serializable for every class?

Now when I try to access variables inside PlayerDetials class as below I am getting a Null Exception...

That's because the resObject variable is null or player_details is null . You have to check if resObject is null then check if the Length of player_details is > 0 before accessing it. Since this is a List , you have to use player_details.Count to perform this check.

These variables can be null when JsonUtility.FromJson fail to de-serialize the json because your json does not match the class or when the json does not even match the classes in your question. It can also be null when you are missing [Serializable] .

You can find out if your json matches your classes by posing it here . Please, do this before commenting under this answer.

Am I missing something, Do I need to compulsory declare in above manner?

No . You don't need to do it the way you are doing it now. Since you are converting string/json to object, there is absolutely no need to initialize the variable before calling JsonUtility.FromJson .

This is fine:

private ResponseObject resObject;

void yourFunction()
{
    resObject = JsonUtility.FromJson<ResponseObject>(m_apiCallResponse);
}

Or

void yourFunction()
{
    ResponseObject resObject  = JsonUtility.FromJson<ResponseObject>(m_apiCallResponse);
}

Now, if you are using JsonUtility.ToJson , the variable you pass to this function must be initialized since you are converting that variable object into string/json.

And do I need to place [Serializable] on every class that I declare to make JsonUtility work

Yes . You need that in order to serialize/deserialize each class with JsonUtility .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM