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 WesterlyCarrot9 · May 19, 2014 at 11:27 PM · javascriptguiinventoryrpgitem

Real Simple Inventory and Vendor System Help

Ok so i'm trying to create a very simple inventory and vendor system using the scripts below. Right now, i'm stuck at the point where i want to just click on an item from the vendor window and then the GUI button of this item disappears from him and appears in the player's inventory. I tried to use the "GetComponent" method which i've used successfully on other scripts but with no luck. Is there any other way to do such a thing? Of course, i'd love to have a drag-and-drop system but that's a little more advanced than what i want right now. Thanks!

 Vendor Script.js
 
 var range : float;
 var player : GameObject;
 
 var close : boolean = false;
 var vendorwindow : boolean = false;
 
 function OnMouseOver(){
     if((Input.GetMouseButtonDown(0)) && close){
         vendorwindow = true;
     }    
 }
 
 function Update(){
     if(Vector3.Distance(player.transform.position, transform.position)<=range){
         close = true;
     }    
     else{
         close = false;
         vendorwindow = false;
     }
 }
 
 function OnGUI(){
     if(vendorwindow){
         GUI.Box(new Rect(20,10,160,160), "SHOP");
         
         if(GUI.Button(new Rect(40,40,50,40),"Sword")){
             Debug.Log("I bought a sword!");
         }
         if(GUI.Button(new Rect(40,90,50,40),"Shield")){
             Debug.Log("I bought a shield!");
         }
         if(GUI.Button(new Rect(110,40,50,40),"Bow")){
             Debug.Log("I bought a bow!");
         }
         if(GUI.Button(new Rect(110,90,50,40),"Arrows")){
             Debug.Log("I bought some arrows!");
         }    
     }
 }

 Player Inventory.js
 
 var openINV : boolean = false;
 
 function Update(){
     if(Input.GetKeyDown(KeyCode.I)){
         openINV = !openINV;
     }    
 }
 
 function OnGUI(){
     if(openINV){
         GUI.Box(new Rect(Screen.width - 180,Screen.height - 180,160,160),"INVENTORY");
         
         GUI.Box(new Rect(Screen.width - 160,Screen.height - 150,50,40),"");
         GUI.Box(new Rect(Screen.width - 160,Screen.height - 100,50,40),"");
         GUI.Box(new Rect(Screen.width - 90,Screen.height - 150,50,40),"");
         GUI.Box(new Rect(Screen.width - 90,Screen.height - 100,50,40),"");
     }
 }

 
Comment
Add comment · Show 1
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 Clet_ · May 19, 2014 at 11:45 PM 0
Share

You can't hard-code something that is dynamically changing...

Your inventory/vendor list of items shouldn't be a sequence of ifs. It should be a list that you iterate through

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Clet_ · May 19, 2014 at 11:57 PM

Edit : Just noted your answer was in JS. I don't know shit about JS, but it should be easy to translate.

You should have a class named item that looks like this :

 public class Item {
     private string _name;
     public string name {
         get { return _name; }
     }
     
     public Item(string name) {
         if(string.IsNullOrEmpty (name)) {
             throw new System.ArgumentNullException("name");
         }
 
         _name = name;
     }
 
     public void OnItemBought () {
         Debug.Log (string.Format "You bought a {0}!", _name);
     }
 }

Then, you'd need to have a vendor and an inventory class who both has a list of items :

 List<Item> items = new List<Item>();

After the vendor list has been initialized i.e. every item it can sell has been added to its item list. You'd be able to display dynamically the list with a function that looks something like :

 for(int i = 0; i < vendor.items.Count; i++) {
     if(GUIButton(new Rect(40, 40 + 50*i, 50, 40), vendor.items[i].name)) {
         inventory.Add (vendor.items[i]);
         vendor.items[i].OnItemBought ();
         vendor.items.RemoveAt (i--);
     }
 }

The code won't work as is. It's up to you to apply this concept to your specific environment

Comment
Add comment · Show 2 · 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 WesterlyCarrot9 · May 20, 2014 at 12:21 AM 0
Share

No worries. I've wrote some little scripts in C# as well so it should be easy to understand your answer. It's just that i've never worked with Lists before.

avatar image Clet_ · May 20, 2014 at 01:03 AM 0
Share

You should really explore Generic collections. Take a day off and look into Dictionary, List, Stack, Queue, LinkedList, HashSet, etc.

Thank me later.

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

21 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

Related Questions

Item/inventory system-equipable items 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Can't Assign Item In Array 1 Answer

Problem with dropping items from my inventory 0 Answers

Crafting system 2 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