Search Unity

Using JSON List to populate Shop Scroll List

Discussion in 'Scripting' started by Ronin5150, Dec 17, 2019.

  1. Ronin5150

    Ronin5150

    Joined:
    Sep 18, 2017
    Posts:
    4
    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.
    Code (CSharp):
    1. using
    2. UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. using System.Collections.Generic;
    6.  
    7. [System.Serializable]
    8. public class Titles
    9. {
    10.     public string name;
    11.     public string address;
    12.     public string iapid;
    13. }
    14.  
    15. public class ShopScrollList : MonoBehaviour
    16. {
    17.  
    18.     public Transform contentPanel;
    19.     public SimpleObjectPool buttonObjectPool;
    20.     public List<Titles> TitlesList = new List<Titles>();
    21.  
    22.  
    23.     // Use this for initialization
    24.     void Start()
    25.  
    26.     {
    27.         TextAsset asset = Resources.Load("Catalog") as TextAsset;
    28.         if (asset != null)
    29.         {
    30.          
    31.             titlesList = JsonUtility.FromJson<Titles>(asset.text);
    32.         }
    33.     {
    34.         RefreshDisplay();
    35.     }
    36.  
    37.     void RefreshDisplay()
    38.     {
    39.         RemoveButtons();
    40.         AddButtons();
    41.     }
    42.  
    43.     void RemoveButtons()
    44.     {
    45.         while (contentPanel.childCount > 0)
    46.         {
    47.             GameObject toRemove = transform.GetChild(0).gameObject;
    48.             buttonObjectPool.ReturnObject(toRemove);
    49.         }
    50.     }
    51.  
    52.     void AddButtons()
    53.     {
    54.         for (int i = 0; i < titlesList.Count; i++)
    55.         {
    56.             Titles titles = titlesList[i];
    57.             GameObject newButton = buttonObjectPool.GetObject();
    58.             newButton.transform.SetParent(contentPanel);
    59.  
    60.             SampleButton sampleButton = newButton.GetComponent<SampleButton>();
    61.             sampleButton.Setup(titles, this);
    62.         }
    63.     }
    64. }
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class SampleButton : MonoBehaviour
    7. {
    8.     public Button button;
    9.     public Text name;
    10.    
    11.  
    12. }
    13.  

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. // A very simple object pooling class
    5. public class SimpleObjectPool : MonoBehaviour
    6. {
    7.     // the prefab that this object pool returns instances of
    8.     public GameObject prefab;
    9.     // collection of currently inactive instances of the prefab
    10.     private Stack<GameObject> inactiveInstances = new Stack<GameObject>();
    11.  
    12.     // Returns an instance of the prefab
    13.     public GameObject GetObject()
    14.     {
    15.         GameObject spawnedGameObject;
    16.  
    17.         // if there is an inactive instance of the prefab ready to return, return that
    18.         if (inactiveInstances.Count > 0)
    19.         {
    20.             // remove the instance from the collection of inactive instances
    21.             spawnedGameObject = inactiveInstances.Pop();
    22.         }
    23.         // otherwise, create a new instance
    24.         else
    25.         {
    26.             spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
    27.  
    28.             // add the PooledObject component to the prefab so we know it came from this pool
    29.             PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
    30.             pooledObject.pool = this;
    31.         }
    32.  
    33.         // put the instance in the root of the scene and enable it
    34.         spawnedGameObject.transform.SetParent(null);
    35.         spawnedGameObject.SetActive(true);
    36.  
    37.         // return a reference to the instance
    38.         return spawnedGameObject;
    39.     }
    40.  
    41.     // Return an instance of the prefab to the pool
    42.     public void ReturnObject(GameObject toReturn)
    43.     {
    44.         PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
    45.  
    46.         // if the instance came from this pool, return it to the pool
    47.         if (pooledObject != null && pooledObject.pool == this)
    48.         {
    49.             // make the instance a child of this and disable it
    50.             toReturn.transform.SetParent(transform);
    51.             toReturn.SetActive(false);
    52.  
    53.             // add the instance to the collection of inactive instances
    54.             inactiveInstances.Push(toReturn);
    55.         }
    56.         // otherwise, just destroy it
    57.         else
    58.         {
    59.             Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
    60.             Destroy(toReturn);
    61.         }
    62.     }
    63. }
    64.  
    65. // a component that simply identifies the pool that a GameObject came from
    66. public class PooledObject : MonoBehaviour
    67. {
    68.     public SimpleObjectPool pool;
    69. }
    70.  
    Any help would be greatly appreciated. I've been working it every which way for over a week. Thank you
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    757
    C# is case sensitive. You define TitlesList and are using titlesList in your methods.
     
    Ronin5150 likes this.
  3. Ronin5150

    Ronin5150

    Joined:
    Sep 18, 2017
    Posts:
    4
    Thank you for the fast reply. I'll clean this up and see what errors it throws from there.
    Awesome....
     
  4. Ronin5150

    Ronin5150

    Joined:
    Sep 18, 2017
    Posts:
    4
    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<Titles>'
    Here is the entire script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using System.Collections.Generic;
    5.  
    6. [System.Serializable]
    7. public class Titles
    8. {
    9.     public string name;
    10.     public string address;
    11.     public string iapid;
    12. }
    13.  
    14. public class ShopScrollList : MonoBehaviour
    15. {
    16.  
    17.     public Transform contentPanel;
    18.     public SimpleObjectPool buttonObjectPool;
    19.     public List<Titles> TitlesList = new List<Titles>();
    20.  
    21.  
    22.     // Use this for initialization
    23.     void Start()
    24.  
    25.     {
    26.         TextAsset asset = Resources.Load("Catalog") as TextAsset;
    27.         if (asset != null)
    28.         {
    29.          
    30.             TitlesList = JsonUtility.FromJson<Titles>(asset.text);
    31.         }
    32.     {
    33.         RefreshDisplay();
    34.     }
    35.  
    36.     void RefreshDisplay()
    37.     {
    38.         RemoveButtons();
    39.         AddButtons();
    40.     }
    41.  
    42.     void RemoveButtons()
    43.     {
    44.         while (contentPanel.childCount > 0)
    45.         {
    46.             GameObject toRemove = transform.GetChild(0).gameObject;
    47.             buttonObjectPool.ReturnObject(toRemove);
    48.         }
    49.     }
    50.  
    51.     void AddButtons()
    52.     {
    53.         for (int i = 0; i < TitlesList.Count; i++)
    54.         {
    55.             Titles titles = TitlesList[i];
    56.             GameObject newButton = buttonObjectPool.GetObject();
    57.             newButton.transform.SetParent(contentPanel);
    58.  
    59.             SampleButton sampleButton = newButton.GetComponent<SampleButton>();
    60.             sampleButton.Setup(titles, this);
    61.         }
    62.     }
    63. }
    64. }
     
  5. Hikiko66

    Hikiko66

    Joined:
    May 5, 2013
    Posts:
    1,020
    TitlesList is of type List<Titles>

    So it wants you to assign it that type
    "Cannot implicitly convert type 'Titles' to 'System.Collections.Generic.List<Titles>"

    Code (CSharp):
    1. TitlesList = JsonUtility.FromJson<List<Titles>>(asset.text);
     
    Ronin5150 likes this.
  6. Ronin5150

    Ronin5150

    Joined:
    Sep 18, 2017
    Posts:
    4
    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.
     
unityunity