- Home /
The question is answered, right answer was accepted
Resources.Load returns empty parameters?
I'm working on an inventory script that uses a set pool of item IDs to get items and their stats based on the ID of the UI.Button in your menu. Essentially, I have 15 buttons arranged in a grid. Each of them starts off with a set item ID. (At the moment, there's only 4 IDs, including the empty ID.)
When function Start() is called on the button, it asks the item pool "My Item ID is X, what item am I?" The item pool then returns the name, weight, max stack size, and logo of the appropriate item. The way I want this to work is so I can add items to the game with ease by simply adding a new ID to the end of the script. So far, the script runs perfectly. The buttons adopt all the stats except for the logo. When I attempt to use Resources.Load to get an image for the item, I apply the Sprite to an array, which is sent back to the button in a BroadcastMessage function. The buttons fail to call function GetItemImage(image:Sprite) because GetItemImage(image:Sprite) has an empty parameter when it requires a Sprite. For the life of me, I can't get Resources.Load to load the appropriate sprites.
item_buttons.js
#pragma strict
private var itemPool : GameObject;
var itemID = 0;
var itemName : String = "No Item";
var itemWeight : int = 0;
var stackCount : int = 1;
var maxStackCount : int = 1;
private var stackText : UI.Text;
private var itemImage : UI.Image;
function Start () {
stackText = transform.Find("#TEXT:StackText").GetComponent.<UI.Text>();
itemImage = transform.Find("#IMAGE:ItemImage").GetComponent.<UI.Image>();
itemPool = GameObject.FindWithTag("ItemPool");
itemPool.GetComponent(item_ids).RequestItemFromID(gameObject, itemID);
UpdateStackText();
}
function Update () {
}
function UpdateStackText () {
stackText.text = " " + stackCount.ToString();
}
function GetItemName(name : String) {
itemName = name;
}
function GetItemWeight(weight : int) {
itemWeight = weight;
}
function GetItemStack(stack : int) {
stackCount = stack;
}
function GetMaxItemStack(maxStack : int) {
maxStackCount = maxStack;
}
function GetItemImage(image : Sprite) {
itemImage.sprite = image;
}
item_ids.js
#pragma strict
function RequestItemFromID (target:GameObject,itemID:int) {
var itemStats = new GetStatsFromID(itemID);
target.BroadcastMessage("GetItemName", itemStats[0]);
target.BroadcastMessage("GetItemWeight", itemStats[1]);
target.BroadcastMessage("GetMaxItemStack", itemStats[2]);
target.BroadcastMessage("GetItemImage", itemStats[3]);
}
//===
//Get stats below this point
//===
function GetStatsFromID (itemID : int) {
var stats = new Array();
if(itemID == 0) {
stats[0] = "No Item";
stats[1] = 0;
stats[2] = 1;
stats[3] = Resources.Load("00_no_item") as Sprite;
}
else if(itemID == 1) {
stats[0] = "Stone Arrowhead";
stats[1] = 1;
stats[2] = 99;
stats[3] = Resources.Load("01_stone_arrowhead") as Sprite;
}
else if(itemID == 2) {
stats[0] = "Iron Arrowhead";
stats[1] = 1;
stats[2] = 99;
stats[3] = Resources.Load("02_iron_arrowhead") as Sprite;
}
else if(itemID == 3) {
stats[0] = "Stick";
stats[1] = 2;
stats[2] = 99;
stats[3] = Resources.Load("03_stick") as Sprite;
}
return stats;
}
Answer by DrShikura · Sep 06, 2015 at 04:31 PM
I fixed the issue! I re-wrote my scripts to return a String when asking for the image in stead of using Resources.Load every time you want to call a new ID. Now, Resources.Load is only called when you call function GetItemImage(imagepath:String). I also re-wrote the Resources.Load line to ask specifically for the Sprite in stead of finding the file and making it a sprite.
This attempts to load any file in Resources by the name of "00_no_item." If found, it will attempt to convert that file into a Sprite. The issue with this is as follows; If you fail to convert the object into a Sprite, it doesn't return any form of error. It simply returns the variable as null.
Resources.Load("00_no_item") as Sprite;
This one specifically searches the Resources folder for a Sprite by the name of "00_no_item." If not found, it will return null. However, no conversions are being made here, so if it finds a file, it is finding the correct file. My error before was that it was finding a GameObject by the same name and was failing to convert it to type Sprite.
Resources.Load.<Sprite>("00_no_item");
Now, the scripts look like this:
item_ids.js
function GetStatsFromID (itemID : int) {
var stats = new Array();
if(itemID == 0) {
stats[0] = "No Item";
stats[1] = 0;
stats[2] = 1;
stats[3] = "item_logos/00_no_item";
}
else if(itemID == 1) {
stats[0] = "Stone Arrowhead";
stats[1] = 1;
stats[2] = 99;
stats[3] = "item_logos/01_stone_arrowhead";
}
else if(itemID == 2) {
stats[0] = "Iron Arrowhead";
stats[1] = 1;
stats[2] = 99;
stats[3] = "item_logos/02_iron_arrowhead";
}
else if(itemID == 3) {
stats[0] = "Stick";
stats[1] = 2;
stats[2] = 99;
stats[3] = "item_logos/03_stick";
}
return stats;
}
item_buttons.js
function GetItemImage(imagepath : String) {
itemImage.sprite = Resources.Load.<Sprite>(imagepath);
}