Array Index Question

Hi all.
I try since 2 Days to get this foreach loop working.

  public void CreateSoftwareCopy()
        // copy software to characterlistprefab

        Getsoftwarepannel = GO.transform.Find("DeckSoftwarePannel").gameObject;
        GetSlotscripts = Getsoftwarepannel.GetComponentsInChildren<SlotScript>();
        foreach(SlotScript script in GetSlotscripts)
            *int ID = script.SlotID;*
            SoftwareCopy = Instantiate(PlayerDataController.SoftwareItem[*ID*]);
       // SoftwareCopy.GetComponent<Canvas>().sortingOrder = 3;
            SoftwareCopy.transform.parent = script.transform;

I have problems with the parts are in **.

I also Tried

SoftwareCopy = Instantiate(PlayerDataController.SoftwareItem[scripts.SlotID])

But as long as i do not enter an fix Array Index (i.e.: [0])
i alwayse get the Error:
"The Object you want to instantiate is null. "

So for me it looks like im not able to get in touch with this ID int, which is in each slotscript as Int, and is also populated. (i checked it and the Slot ID is there with the right IDnumbers).

So, how do i get hand on it, and get the right objects instanciated?

Im not able to get a reference to the script or the Object to get the SlotID int.

Please help me with that, would be verry kind.



Since two days? What have you already tried to debug your problem? The first thing you should have done is adding a Debug.Log to print out that ID. Also since your error is that the object you want to instantiate is null we already know that the ID seems to be a valid index into your “SoftwareItem” array as we do not get an IndexOutOfBounds exception. So you may want to check if the item is actually set to an instance.

foreach(SlotScript script in GetSlotscripts)
    int ID = script.SlotID;
    Debug.Log("Going to instantiate SoftwareItem #" + ID);
    var prefab = PlayerDataController.SoftwareItem[ID];
    if (prefab == null)
        Debug.LogWarning("SoftwareItem at index " + ID + " is null; skipping!");
    SoftwareCopy = Instantiate(prefab);
    // ... 

Generally avoid Debug.Logs that only print a single value. If you have multiple of them you don’t know where it comes from.

With the changes above do you get the warning we print? If so you directly see which index is the problem. If you check your array you will see that the object at this index is either not set or has been destroyed.

HI, thanks, but i allready checked this, i set the

 //   *int ID = script.SlotID;*
 SoftwareCopy = Instantiate(PlayerDataController.SoftwareItem[0]);

Index direktly to 0 and the Item has been instanciated correctly, but has been instantiated as often as the foreachloop run, in this case 2 times.

I do have 2 slots, which have the ID 0 and 1, and each of them has a seperate Item, and i want to instantiate them both, not the first one 2 times.

So the Array is correct, i only do not get the ID from the “script”.
Oh, and im a newby, Working only some hours a Day on this stuff, and am Stuck now :slight_smile:



P.S. Thanks for the fast reply