Drawing a variable texture2d -

I’m probably doing something completely wrong here, but I’m trying to draw a texture to the screen based on an array with a variable dimension…

	var currentItem : Texture2D;
	currentItem = itemArray[itemUsing] + "_hud";

So, current item then should be something like sword_hud, which is the name of one of my 2d textures and will be drawn in the next line. However, I get the error that I “cannon cast from source type to destination type”.

Any thoughts has to how to make this happen?

you try to assign a String (the right part of your equation) to a Texture2D type. You can’t just use the name of an object to get it from your library of assets. The texture must be in the ‘resources’ folder and you must use the Resources.Load() method like this to retrieve it :

currentItem = Resources.Load (itemArray[itemUsing] + "_hud") ;

Awesome - now I’m not getting that error, but I am getting an error that says object reference not set to an instance of an object. That happens in my draw code - basically:

var currentItem = Resources.Load (itemArray[itemUsing] + "_hud") ;

GUI.Label(Rect(Screen.width-hudElementOffset.x,Screen.height-hudElementOffset.y,currentItem.width, currentItem.height),currentItem);

Any thoughts? or maybe there is a better way to do this altogether?

SOLVED:

Nevermind - needed to add a “resources” folder just as you said, and put those in the resources folder, then it worked without issue. Thanks so much!