I am trying to dynamically load textures using the WWW class in one scene and then send the textures to the next scene. I have a static “textures” array but when I try to get it in next scene, it just gives a null reference exception.
The image loader
using UnityEngine;
using System.Collections;
using System;
public class ImageLoader : MonoBehaviour {
private string url = "";
private static Texture2D[] textures;
private int c = 0;
IEnumerator Start() {
float start = System.DateTime.Now.Millisecond;
textures = new Texture2D[10];
WWW www;
this.GetComponent<MenuBuilder>().drawLoadingScreen();
for(int i = 0; i < 10; i++){
url = "file://"+Application.dataPath+"/Images/"+i+".jpg";
if(!System.IO.File.Exists(Application.dataPath+"/Images/"+i+".jpg")) continue;
www = new WWW(url);
//Debug.Log(i);
textures[c++] = www.texture;
yield return null;
}
Debug.Log(System.DateTime.Now.Millisecond - start);
this.GetComponent<MenuBuilder>().drawMainMenu();
DontDestroyOnLoad(this);
}
public static Texture2D getTexture(int i){
return textures*;*
- }*
- public int getNumberOfTextures(){*
-
return c;*
- }*
}
The class in next scene trying to access the images
using UnityEngine;
using System.Collections;
public class CubeHandler : MonoBehaviour {
-
public GUITexture cubePrefab;*
-
public GameObject handlePrefab;*
-
public Transform handleParent;*
-
public Transform cubeParent;*
-
public float difficulty;*
-
public Texture2D spriteMap;*
-
public static int size;*
-
public float boardSize;*
-
private static GUITexture[,] cubeArray;*
-
private static GameObject[,] handles;*
-
public static GUITexture[,] getCubeArray(){*
-
return cubeArray;*
-
}*
-
public static GameObject[,] getHandles(){*
-
return handles;*
-
}*
-
void Start () {*
-
difficulty = PlayerPrefs.GetInt("Difficulty");*
_ size = 4*(int)difficulty;_
_ boardSize = 8*difficulty;_
-
//this.transform.rotation = Quaternion.Euler(90,0,0);*
-
this.transform.position = new Vector3(-1,7,1); // Camera Position*
-
cubeArray = new GUITexture[size,size];*
-
handles = new GameObject[2,size];*
-
for(int i = 0; i <2; i++){*
-
for(int j = 0; j < size;j++){*
-
if(i == 1)*
-
handles[i,j] = Instantiate(handlePrefab,getHandlePosition(i,j),Quaternion.identity) as GameObject;*
-
else*
-
handles[i,j] = Instantiate(handlePrefab,getHandlePosition(i,j),Quaternion.identity) as GameObject;*
-
handles[i,j].GetComponent<HandleData>().index = new Vector2(i,j);*
_ handles[i,j].transform.localScale*=((float)boardSize/(float)size)/2.0f;_
-
handles[i,j].transform.parent = handleParent;*
-
}*
-
} *
-
AnimatedTextureExtendedUV a;*
-
CubeData c;*
-
for(int i = 0; i < size;i++){*
-
for(int j = 0; j < size;j++){*
-
cubeArray[i,j] = Instantiate(cubePrefab,getCubePosition(i,j),Quaternion.identity) as GUITexture;*
-
Debug.Log(PlayerPrefs.GetInt("Texture"));*
-
cubeArray[i,j].texture = ImageLoader.getTexture(PlayerPrefs.GetInt("Texture"));*
-
// NULL REFERENCE Exception*
_ cubeArray[i,j].transform.localScale*=((float)boardSize/(float)size)/2.0f;_
-
cubeArray[i,j].transform.parent = cubeParent;*
-
if(cubeArray[i,j]){*
-
c = cubeArray[i,j].GetComponent<CubeData>();*
-
c.originalIndex = c.currentIndex = new Vector2(i,j);*
-
a = cubeArray[i,j].GetComponent<AnimatedTextureExtendedUV>();*
-
a.fps = 0;*
-
a.colCount = size;*
-
a.rowCount = size;*
_ a.totalCells = size*size;_
-
a.rowNumber = i;*
-
a.colNumber = j;*
-
}else{*
-
Debug.LogError("Array index is null");*
-
}*
-
}*
-
}*
-
// shuffling tiles*
-
//GameLogic.shuffle();*
-
}*
-
Vector3 getHandlePosition(int axis,int number){*
-
Vector3 position;*
-
float boxlength = boardSize/size;*
-
if(axis == 0)*
_ position = new Vector3(boxlength*number-boardSize/2,0,-boardSize/2+boxlength/2-1);_
-
else*
_ position = new Vector3(boardSize/2-boxlength/2+1,0,boxlength*number-boardSize/2+boxlength);_
-
return position; *
-
}*
-
Vector3 getCubePosition(int row, int column){*
-
Vector3 position;*
-
float boxlength = boardSize/size;*
_ position = new Vector3(boxlength/2+boxlengthrow-boardSize/2,0,boxlength/2+boxlengthcolumn-boardSize/2);_
-
return position;*
- }*
}
What should I do?