3

I have many image in folder screenshots. So this script will be calling the image from folder without applying. But the thing is, the image only displays one image. I mean they did not go to next image like slide show or fade in/out.

Then I keep getting this error message:

IndexOutOfRangeException: Index was outside the bounds of the array.

It points to this code:

ImageHolder[i].GetComponent<RawImage>().texture = thisTexture;

Full code:

Texture2D thisTexture;
byte[] bytes;
string fileName;

public GameObject[] ImageHolder = new GameObject[1];
    
void Start()
    {
        var imagesToLoad = Directory.GetFiles(Application.dataPath + "/screenshots", "*.png");
        for (int i = 0; i < imagesToLoad.Length; i++)
    
        {
            thisTexture = new Texture2D(100, 100); //NOW INSIDE THE FOR LOOP
            fileName = imagesToLoad[i];
            bytes = File.ReadAllBytes(fileName);
    
            thisTexture.LoadImage(bytes);
            thisTexture.name = fileName;
    
    
            // This line
            ImageHolder[i].GetComponent<RawImage>().texture = thisTexture;
        }
    }
}

Here I attached the output. I have 4 RawImage's. The image just displays and gets stuck.

enter image description here

chimmy12
  • 29
  • 5
  • 1
    could you please provide some more details to your question (essential code). – Vickel Dec 22 '21 at 02:36
  • 1
    also please note: Code Snippets are not meant to be used to format code! Code Snippets are run-able code blocks to create a minimal, complete, and verifiable example! – Vickel Dec 22 '21 at 02:36
  • 2
    You initialize an array with the size of one. Your GameObjects are not initialized and if your path contains more than one image this will also fail, even if the GameObject is initialized. – S. Stumm Dec 22 '21 at 02:40
  • 1
    ImageHolder contains 1 element and you're most likely looping through more than 1 item in the for loop. So obviously the imagesToLoad.Length is > 1 so when the for loop reaches 2nd element which is i = 1 the code fails. – Desolator Dec 22 '21 at 02:50
  • Noe UnityScript(js) is pretty much dead, you should look at migrating to C# – Jon P Dec 22 '21 at 02:59
  • hi. thankyou for your response! so act I have one task that I need to display many image (1000++) in REALTIME. Then I need to display the image without applying elements, without button, without trigger. Just automatic like slideshow. Let say I have 6 RawImage in unity, then the image will display the Rawimage by this script. I have successfully display it but I cant make it go to next image. Like first 6 image will display and gone then come with new 6 image. @Vickel – chimmy12 Dec 22 '21 at 03:04
  • HIIII I want to update the error was solved. But I still cant make the image will display to the next image one by one in Realtime – chimmy12 Dec 22 '21 at 03:55

2 Answers2

0

Update!! the error was solved!

I put the variable at image holder

from this

*ImageHolder[i].GetComponent<RawImage>().texture = thisTexture;

to

*ImageHolder[9].GetComponent<RawImage>().texture = thisTexture;

but only display at RawImage(9) and still stuck with one image.

chimmy12
  • 29
  • 5
0

As I understood you fill the array ImageHolder with GameObjects in the Editor. Hence you could remove the not relevant and confusing assignment of new GameObject[1] when you initalize the field.

But you should break out of the for loop if your ImageHolder array is shorter than imagesToLoad array.

Or account for it differently by expanding the array and creating new ImageHolders GameObject in which case you probably want to use a list. You could directly reference the RawImage and create a Prefab so you can create and fill your list more easily.

Instead of breaking out of the for loop you could just resize the imagesToLoad array or if you want to use the first use .First() or a select a specific range.

Juri Knauth
  • 359
  • 1
  • 4
  • 11