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