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 Dezzy · Apr 04, 2013 at 02:50 PM · inventoryitem

Help making premade Items for farming game

Hi, I've been trying to make items for my farming simulation game (such as seeds, tools etc) but I've hit a snag when trying to make pre-made items the player can get. Originally, the Item class inherited from MonoBehaviour and attached to prefabs, which I could then drag into the Inventory class in the inspector of whatever would contain the item (e.g. a shop).

However, I ran into problems creating a new instance of the object (MonoBevahiour doesn't like using 'new Item()') and copying the prefab would create problems with the 'amount' variable, where every item copied from the same prefab would have the same value for amount. Uninheriting Monobehaviour from the Item class allows me to create a new Instance of the Item class, but I can no longer attach the Item class to a prefab.

Is there a way I can create a list of Items, which could then be assigned in the Inventory of whatever would have the Item? Or is there a way I can make a new Instance of a prefab (Item class inheriting from MonoBehaviour), with each copy of an item being independent to another. The classes I am using are below.

 class Item //Item Class
 {
     var ID : int;
     var price : int;
     var stackability : int;
     var image : Texture2D;
     var amount : int = 1;
     var selected : boolean = false;
     var worldObject : GameObject;
     
     function PassItem(newItem : Item)
     {
         price = newItem.price;
         stackability = newItem.stackability;
         image = newItem.image;
         worldObject = newItem.worldObject;
         selected = false;
     }
 }

 //Inventory Class
 import System.Collections.Generic;
 
 var inventoryItems : Item[];
 var size : int;
 
 function Awake()
 {
     if (size != 0)
         inventoryItems = new Array(size);
 }
 
 function AddItem(newItem : Item) : boolean
 {
     if (CheckItem(newItem)) return true;
     else if (inventoryItems.Count == size) return false;
     else
     {
         for (var i = 0; i < inventoryItems.length; i++) 
         { 
             if( inventoryItems[i] == null ) 
             {
                 var item = new Item();
                 item.PassItem(newItem);
                 inventoryItems[i] = item;
                 inventoryItems[i].amount = 1;
                 return true;
             }
         }
     }
     return false;
 }
 
 function CheckItem(desiredObject : Item)
 {
     for (var i = 0; i < inventoryItems.length; i++)
     {
         if (inventoryItems[i] != null)
         {
             if (inventoryItems[i].ID == desiredObject.ID)
             {
                 if(inventoryItems[i].amount < desiredObject.stackability)
                 {
                     inventoryItems[i].amount += 1;
                     return true;
                 }
             }
         }
         if (i == inventoryItems.length - 1)
             return false;
     }
     return false;
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Nachtmahr · Apr 04, 2013 at 03:21 PM

 [Serialize]
 public class Item{
 ....
 }
 
 public class Inventory : MonoBehaviour{
  public Item[] items;
 ...
 }

;P Try it if you dont have any Complex Types it will Serialize very Well.

Edit: Sry I see you use JS this is the C# Solution. I dont know how Serialize on JS works.

Comment
Add comment · Show 1 · 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
avatar image Dezzy · Apr 04, 2013 at 03:36 PM 0
Share

Thanks anyway. I was looking into Serializing by inheriting the Item Class from Object/ScriptableObject, but I'm not sure on where to go from there.

avatar image
0

Answer by Dezzy · Apr 04, 2013 at 06:43 PM

I think I have found the answer. I made the Item Class inherit from ScriptableObject and wrote a custom EditorWindow script that makes a new instance of the Item Class and creates an asset of it in the Project directory. This way allows me to drag and drop the ScriptableObject into anywhere in the scene that takes an Item object.

If anyone wants the script for this, the JavaScript version is below (shouldn't be too hard to convert to C#).

 import UnityEngine;
 import UnityEditor;
 import System.Collections;
 
 public class ItemDatabaseManager extends EditorWindow
 {    
     @MenuItem("RPG/Item Database Manager")
     static function Init()
     {
         var window:ItemDatabaseManager = EditorWindow.CreateInstance(ItemDatabaseManager);        
         window.Show();
     }
     
     var newItemName:String = "";
     var newItemID:int = 0;
     var newItemPrice:int = 0;
     var newItemStackability:int = 1;
     var newItemImage:Texture2D;
     var newItemAmount:int = 1;
     var newItemPrefab:GameObject;
 
     function OnGUI()
     {
         newItemName = EditorGUILayout.TextField("Name: ", newItemName);
         newItemID = EditorGUILayout.IntField("ID: ", newItemID);
         newItemPrice = EditorGUILayout.IntField("Price: ", newItemPrice);
         newItemStackability = EditorGUILayout.IntField("Stackability: ", newItemStackability);
         newItemPrefab = EditorGUILayout.ObjectField("Prefab: ", newItemPrefab, GameObject);
         newItemImage = EditorGUILayout.ObjectField("Image: ", newItemImage, Texture2D);
         
         if (GUILayout.Button("Add New Item"))
         {
             var newItem : Item = ScriptableObject.CreateInstance(Item);
             newItem.ID = newItemID;
             newItem.price = newItemPrice;
             newItem.stackability = newItemStackability;
             newItem.worldObject = newItemPrefab;
             newItem.image = newItemImage;
             AssetDatabase.CreateAsset(newItem, "Assets/ItemDatabase/" + newItemName +".asset");
             AssetDatabase.SaveAssets();
         }
     }
 }
 
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

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

11 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

Related Questions

Adding Item to an Inventory. 1 Answer

Unable to usem item from inventory 1 Answer

How can I transfer the item's image in inventory UI to the next scene?,How can I keep and transfer the image of items in the inventory in different scenes? 0 Answers

Invoking Effects of (Random) Items (C#) 0 Answers

[C#]Inventory script help. 3 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