Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Sep 06, 2015 at 04:31 PM by DrShikura for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DrShikura · Sep 06, 2015 at 02:39 PM · resources.loadinventory systemitemsparameterbroadcastmessage

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.


alt text alt text


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;
 }


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

1 Reply

  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Share
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

Follow this Question

Answers Answers and Comments

27 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I send a Transform as parameter with BroadcastMessage()? 1 Answer

Inventory and scriptable object items - please point me in the right direction 1 Answer

Parse different types of objects from Json 1 Answer

Creating a custom List or Collection 1 Answer

Access to slot Item 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