- Home /
Resources.Load makes Featureless Oddity
Hello,
I am having a problem using code to go through tile atlases at startup and storing them for use when levels are created. The issue is that I seem to be creating a Texture2D, but it has none of the features/attributes it should. Here is the code:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class ArchMage : MonoBehaviour
{
public Dictionary<string, Color[][]> mapTilesets; //One entry is one atlas
private string[] mapAtlasFiles;
private int numberAtlases;
void Start ()
{
GameObject.DontDestroyOnLoad(transform.gameObject);
mapAtlasFiles = Directory.GetFiles("Assets/Resources/Textures/Map");
numberAtlases = mapAtlasFiles.Length;
mapTilesets = new Dictionary<string, Color[][]>();
foreach(string entry in mapAtlasFiles)
{
//Texture2D currentAtlas = (Texture2D)Resources.Load("/Textures/Map/" + entry); //Not registering
Texture2D currentAtlas = Resources.Load(entry) as Texture2D;
//Debug.Log("Entryname: " + entry);
Debug.Log("Atlas: " + currentAtlas.height); //Texture is being created but has no attributes (name, height, etc) used in later functions
//Debug.Log("Atlas: " + currentAtlas); //Displays an entry
//Debug.Log("Texturename: " + currentAtlas.name);
mapTilesets.Add(entry, AtlasTileData(currentAtlas));
}
foreach(var entry in mapTilesets)
{
Debug.Log("Atlas entry: " + entry);
}
}
private Color[][] AtlasTileData(Texture2D atlas)
{
Color[][] atlasTiles = new Color[numberAtlases][];
int x, y;
int a = 0;
for(y = 0; y </*atlas.height / */MapConstants.tileRes/16; y++) //Including atlasHeight causes the error to generate here for NullReference
{ for(x = 0; x < /*atlas.width / */MapConstants.tileRes/16; x++)
{
Color[] tilePixels;
tilePixels = TilePixelData(x * MapConstants.tileRes, y * MapConstants.tileRes, atlas);
atlasTiles[a] = tilePixels;
a++;
}
}
return atlasTiles;
}
private Color[] TilePixelData(int x, int y, Texture2D atlas)
{
Color[] pixels;
int tilePixelSize = MapConstants.tileRes * MapConstants.tileRes;
pixels = new Color[tilePixelSize];
pixels = atlas.GetPixels(x, y, MapConstants.tileRes, MapConstants.tileRes);
System.Array.Reverse(pixels);
return pixels;
}
As you can see in the comments, what happens is this: If I debug for a Texture2D's presence, I get a positive. If I try to access any of its members (height, name, etc) I get a null reference exception, as I presently do when I run the game.
The (only, currently) file I have in there is in Resources -> Textures -> Map and is named Terrain.
Help is appreciated, thank you!
Your answer

Follow this Question
Related Questions
Why does the behavour of my script change when it is built? 0 Answers
Resources.Load ... async ? For large images. 1 Answer
Resources.Load() don't loads texture in standalone build, when in unityeditor it does 1 Answer
assign texture to asset material via script 1 Answer
Checking for null against Texture2D 0 Answers