What is the best way to iterate through a list of Scriptableobjects?
Using the Introduction to Scriptableobjects session I’ve created a Scriptableobject that contains a list of Scriptableobjects, but now I’m wondering how to reach the information inside each of those objects and create a toggle with that information attached as text. I’d say my programming knowledge is beginner to intermediate.
I’m not sure I understand your question. Iterating a list of anything in C# is the same, scriptable object, plain old object, or even a value type like an integer. You just use a for loop, a foreach loop, or a while, whatever meets your needs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PopulateLightSpells : MonoBehaviour {
public List<SpellList> prefSpellList;
public GameObject canvas;
void Start ()
{
DefaultControls.Resources uiResources = new DefaultControls.Resources();
foreach(var Spell in prefSpellList)
{
GameObject uiToggle = DefaultControls.CreateToggle(uiResources);
uiToggle.transform.SetParent(canvas.transform, false);
Debug.Log(prefSpellList.Count);
}
}
}
This however is only creating one toggle when there are 7 elements in the SpellList that I attached to this.
Are you sure it does not create 7 instances? Check the hierarchy. The snippet does not set any offset (not sure if anything else does) so one might just hide the others, as they seem to be created in the exact same place.
i did think about that, there is just the one. I also have a Vertical Layout Group set for that space where I want them to appear so I would expect that to take effect as well.
This definitely shows that the active component’s list contains 1 object. I mean, it’s proven by both, the count that you print and the number of logs that appear.
You should re-check the component again. Perhaps you’ve got an inactive duplicate (or prefab) with 7 objects assigned, whereas the scene contains the one that holds one object.
So I deleted all of the lists i had made so far made a new one and tried again…no change. Thanks for trying to help I’ll play around with it a bit more to see if I can find the problem.