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 MileSplit · Feb 25, 2013 at 03:33 AM · javascriptinventory

Help with Inventory Script

Hello, I made this inventory script for my game. It works great but has one bug.. Somtimes when i pick up an item it duplicates in my inventory... #pragma strict var hand : GameObject;

 var sword : Texture;
 var shovel : Texture;
 var woodBlock : Texture;
 var windowBlock : Texture;
 var stoneBlock : Texture;
 var nukeTex : Texture;
 static var nuke : Texture;
 
 
 
 var blank : Texture;
 
 var house : GUISkin;
 private var inventoryOpen : boolean;
 private var gridValue : float = 0;
 
 
 var inventoryTex : Texture;
 var Grids : Texture[];
 
 function Start () {
 nuke = nukeTex;
 }
 
 function Update () {
     if(Input.GetKeyUp("i")){
         if(inventoryOpen == false){
         inventoryOpen = true;
         }
         else if(inventoryOpen == true){
         inventoryOpen = false;
         }
     }
 }
 
 function OnGUI(){
 GUI.skin = house;
 if(inventoryOpen == true){
       GUI.BeginGroup (new Rect (0,0,360,360),inventoryTex);
       //grid
       gridValue = GUI.SelectionGrid(Rect(30,30,300,300), gridValue,Grids,5);
       
       
       //Apply Sword
       if(Grids[gridValue] == sword){
       PlayerDamge.tool = 2;
       hand.renderer.material.mainTexture = sword;
       AddBlock.level = 1;
       }
       
       //Apply Shovel
       if(Grids[gridValue] == shovel){
       PlayerDamge.tool = 1;
       hand.renderer.material.mainTexture = shovel;
       Debug.Log("Shovel");
       }
       
       //Apply Wood Block
       if(Grids[gridValue] == woodBlock){
       PlayerDamge.tool = 3;
       hand.renderer.material.mainTexture = woodBlock;
       AddBlock.blockTex = woodBlock;
       }
       //Apply windowBlock
       if(Grids[gridValue] == windowBlock){
       PlayerDamge.tool = 3;
       hand.renderer.material.mainTexture = windowBlock;
       AddBlock.blockTex = windowBlock;
             Debug.Log("Window");
             }
             
        //Apply stoneBlock
       if(Grids[gridValue] == stoneBlock){
       PlayerDamge.tool = 3;
       hand.renderer.material.mainTexture = stoneBlock;
       AddBlock.blockTex = stoneBlock;
       }
         //Apply nuke
       if(Grids[gridValue] == nuke){
       PlayerDamge.tool = 3;
       hand.renderer.material.mainTexture = nuke;
       AddBlock.blockTex = nuke;
       }
 
       
       GUI.EndGroup();
       }
 }
 
 
 function OnTriggerEnter (c : Collider){
     if(c.tag == "Item"){
         //Sword
         if(c.renderer.material.mainTexture == sword){
         AddItem(sword);
         Destroy(c.gameObject);
         }
         //Shovel
         if(c.renderer.material.mainTexture == shovel){
         AddItem(shovel);
         Destroy(c.gameObject);
         }
         //Wood Block
         if(c.renderer.material.mainTexture == woodBlock){
         AddItem(woodBlock);
         Destroy(c.gameObject);
         }
         //windowBlock
         if(c.renderer.material.mainTexture == windowBlock){
         AddItem(windowBlock);
         Destroy(c.gameObject);
         }
         //stoneBlock
         if(c.renderer.material.mainTexture == stoneBlock){
         AddItem(stoneBlock);
         Destroy(c.gameObject);
         }
         //nuke
         if(c.renderer.material.mainTexture == nuke){
         AddItem(nuke);
         Destroy(c.gameObject);
         }
     }
 }
 
 function AddItem (itemTex : Texture){
     var i :int;
     for(i=0;i<25;i++){
         if(Grids[i] == blank){
         Grids[i] = itemTex;
         i = 26;
         break;
         }
     }
     
 }



Thanks!, MileSplit

Comment
Add comment · Show 2
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 MileSplit · Feb 26, 2013 at 06:03 PM 0
Share

bunp------

avatar image Tarlius · Feb 26, 2013 at 06:40 PM 0
Share

What exactly do you mean by "duplicates"? Do you mean you can pick up the same item twice, because there seems to be no check for that at all anyway...

1 Reply

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

Answer by Lockstep · Feb 26, 2013 at 06:28 PM

You need to add a check if the item is already in your inventory. This could be done by modifying the AddItem function:

 function AddItem (itemTex : Texture){
     var i :int;
     for(int j =0; j<Grids.length;j++){ //go through every item in Grids
         if(Grids[j] == itemTex)    //if we find the input texture already
             return null;    //exit the AddItem function immediately
     }
     for(i=0;i<25;i++){
         if(Grids[i] == blank){
              Grids[i] = itemTex;
              i = 26; //this is obsolete by the way
              break;
         }
     }
  
 }
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 MileSplit · Feb 26, 2013 at 10:50 PM 0
Share

You sir, are amazing.

Thanks

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

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Item pickup add to inventory? 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Script Not Working 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