Home > Blockchain >  for some reason, i can only generate up to 3 prefabs with this script. otherwise, I get a MissingRef
for some reason, i can only generate up to 3 prefabs with this script. otherwise, I get a MissingRef

Time:07-29

I tried to make a dynamic scroll view to display selectable options that can be both generated or destroyed during runtime but for some reason it only lets me to generate up to three, otherwise I get a Missing Reference Exception.

When I generate one then delete it, it will also give a Missing Reference Exception.

But when I generate two, then delete one or even two, everything works just fine no matter what I do.

How can I fix this strange Behaviour?

The code:

private List<JL> JLList;
public List<GameObject> JLID;

public GameObject ButtonPrefab;
public GameObject PrefabParent;

public GameObject JLMenuManager;

public GameObject ScrollViewContent;

private void Update()
{
    JLList = JLMenuManager.GetComponent<AddJL>().JLList;

    CheckForChange();
}

private void GenerateOptions()
{
    foreach (Transform child in ScrollViewContent.transform)
    {
        Destroy(child.gameObject);
    }

    for (int i = 0; i < JLList.Count; i  )
    {
        GameObject newJLOption = Instantiate(ButtonPrefab, PrefabParent.transform);
        JLID.Add(newJLOption);
        newJLOption = JLID[i];
        int JLIndex = i;
        newJLOption.GetComponent<Button>().onClick.AddListener(() => LoadJLOptions(JLIndex));
    }
}

private void LoadJLOptions(int JLIndex)
{
    SendMessage("SetJL", JLIndex);
    Debug.Log(JLIndex);
}

private void CheckForChange()
{
    int allJLOptions = ScrollViewContent.transform.childCount;
    if (allJLOptions != JLList.Count)
    {
        GenerateOptions();
    }
}

CodePudding user response:

It seems like it is only spawning in how many JLList.Count is set to, open your script JLList and there should be a variable called count . Check how many that is set to. If it is 3, then that is your issue. If not, then follow what @BugFinder said and try remove them from the list

  • Related