Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ClanDestiny · May 03, 2015 at 06:12 PM · texture2dnullreferenceexceptionresources.load

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges