Unity Hangs/Crash when remove GUI Button.

Quick question here. I write a loop in my GUI script and turn out okay when I attach it to a GUI button. As I remove the button so that it would appear straight away, unity hangs. Is there an error in my script? Thank You.

The part of the code:-

if(GUILayout.Button("Add"))
	{
		selStrings = new Texture[StackNum];

		for (i = 0 ; i < selStrings.length ; i++)
		{
			selStrings *= new Texture2D(60,80, TextureFormat.RGB24, false);*
  •  	www = new WWW("file://" + Application.dataPath + "/Screenshots/ScreenShot" + i + ".png");*
    

_ www.LoadImageIntoTexture(selStrings*);*_

_ TextureScale.Bilinear (selStrings*,133,100);
Debug.Log(i);*_

* }*

Is it hanging or just going extremely slow? If you do the above in OnGUI, it will be loading those images at least 2 times every frame.

From the script part you’re showing i can’t determine what language you’re using but i guess it’s UnityScript.

function LoadImages()
{
    selStrings = new Texture[StackNum];

    for (i = 0 ; i < selStrings.length ; i++)
    {
        selStrings *= new Texture2D(60,80, TextureFormat.RGB24, false);*

www = new WWW(“file://” + Application.dataPath + “/Screenshots/ScreenShot” + i + “.png”);

yield www; // this is important! It will suspend this coroutine until the “download” is finished

www.LoadImageIntoTexture(selStrings*);*
TextureScale.Bilinear (selStrings*,133,100);*
Debug.Log(i);
}
}
function OnGUI()
{
if(GUILayout.Button(“Add”))
{
LoadImages();
}
}