Using JSON List to populate Shop Scroll List

Greetings,
This is my first post to the forums.
I am building a shop scrollList based on the Unity tutorial but I am incorporating a JSON file as a source for populating the buttons. I am running into errors when I attempt to use the list created from the JSONUtility.

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

[System.Serializable]
public class Titles
{
    public string name;
    public string address;
    public string iapid;
}

public class ShopScrollList : MonoBehaviour
{

    public Transform contentPanel;
    public SimpleObjectPool buttonObjectPool;
    public List<Titles> TitlesList = new List<Titles>();


    // Use this for initialization
    void Start()

    {
        TextAsset asset = Resources.Load("Catalog") as TextAsset;
        if (asset != null)
        {
          
            titlesList = JsonUtility.FromJson<Titles>(asset.text);
        }
    {
        RefreshDisplay();
    }

    void RefreshDisplay()
    {
        RemoveButtons();
        AddButtons();
    }

    void RemoveButtons()
    {
        while (contentPanel.childCount > 0)
        {
            GameObject toRemove = transform.GetChild(0).gameObject;
            buttonObjectPool.ReturnObject(toRemove);
        }
    }

    void AddButtons()
    {
        for (int i = 0; i < titlesList.Count; i++)
        {
            Titles titles = titlesList[i];
            GameObject newButton = buttonObjectPool.GetObject();
            newButton.transform.SetParent(contentPanel);

            SampleButton sampleButton = newButton.GetComponent<SampleButton>();
            sampleButton.Setup(titles, this);
        }
    }
}

I get the following errors:
Lines 29, 52, and 54 'The name ‘titles.List does not exist win the current context’.
When I try to fix this by making a new instance of the list, I then get an error on the List.Count stating that there is no definition for Count.

Below are the other two scripts in the project.

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

public class SampleButton : MonoBehaviour
{
    public Button button;
    public Text name;
   

}
using UnityEngine;
using System.Collections.Generic;

// A very simple object pooling class
public class SimpleObjectPool : MonoBehaviour
{
    // the prefab that this object pool returns instances of
    public GameObject prefab;
    // collection of currently inactive instances of the prefab
    private Stack<GameObject> inactiveInstances = new Stack<GameObject>();

    // Returns an instance of the prefab
    public GameObject GetObject()
    {
        GameObject spawnedGameObject;

        // if there is an inactive instance of the prefab ready to return, return that
        if (inactiveInstances.Count > 0)
        {
            // remove the instance from the collection of inactive instances
            spawnedGameObject = inactiveInstances.Pop();
        }
        // otherwise, create a new instance
        else
        {
            spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);

            // add the PooledObject component to the prefab so we know it came from this pool
            PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
            pooledObject.pool = this;
        }

        // put the instance in the root of the scene and enable it
        spawnedGameObject.transform.SetParent(null);
        spawnedGameObject.SetActive(true);

        // return a reference to the instance
        return spawnedGameObject;
    }

    // Return an instance of the prefab to the pool
    public void ReturnObject(GameObject toReturn)
    {
        PooledObject pooledObject = toReturn.GetComponent<PooledObject>();

        // if the instance came from this pool, return it to the pool
        if (pooledObject != null && pooledObject.pool == this)
        {
            // make the instance a child of this and disable it
            toReturn.transform.SetParent(transform);
            toReturn.SetActive(false);

            // add the instance to the collection of inactive instances
            inactiveInstances.Push(toReturn);
        }
        // otherwise, just destroy it
        else
        {
            Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
            Destroy(toReturn);
        }
    }
}

// a component that simply identifies the pool that a GameObject came from
public class PooledObject : MonoBehaviour
{
    public SimpleObjectPool pool;
}

Any help would be greatly appreciated. I’ve been working it every which way for over a week. Thank you

C# is case sensitive. You define TitlesList and are using titlesList in your methods.

1 Like

Thank you for the fast reply. I’ll clean this up and see what errors it throws from there.
Awesome…

I cleaned up the case issues in the code and most of the errors are gone.
I have one issue left with the JSONUtility at line 30. It gives the following error:
Assets/ShopScrollList.cs(30,26): error CS0029: Cannot implicitly convert type ‘Titles’ to ‘System.Collections.Generic.List’
Here is the entire script:

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

[System.Serializable]
public class Titles
{
    public string name;
    public string address;
    public string iapid;
}

public class ShopScrollList : MonoBehaviour
{

    public Transform contentPanel;
    public SimpleObjectPool buttonObjectPool;
    public List<Titles> TitlesList = new List<Titles>();


    // Use this for initialization
    void Start()

    {
        TextAsset asset = Resources.Load("Catalog") as TextAsset;
        if (asset != null)
        {
          
            TitlesList = JsonUtility.FromJson<Titles>(asset.text);
        }
    {
        RefreshDisplay();
    }

    void RefreshDisplay()
    {
        RemoveButtons();
        AddButtons();
    }

    void RemoveButtons()
    {
        while (contentPanel.childCount > 0)
        {
            GameObject toRemove = transform.GetChild(0).gameObject;
            buttonObjectPool.ReturnObject(toRemove);
        }
    }

    void AddButtons()
    {
        for (int i = 0; i < TitlesList.Count; i++)
        {
            Titles titles = TitlesList[i];
            GameObject newButton = buttonObjectPool.GetObject();
            newButton.transform.SetParent(contentPanel);

            SampleButton sampleButton = newButton.GetComponent<SampleButton>();
            sampleButton.Setup(titles, this);
        }
    }
}
}

TitlesList is of type List

So it wants you to assign it that type
“Cannot implicitly convert type ‘Titles’ to 'System.Collections.Generic.List”

TitlesList = JsonUtility.FromJson<List<Titles>>(asset.text);
1 Like

Thank you. I tried so many times to get this right, looked on references and videos, etc. Now I get it. Can’t thank you enough.