Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 MrcDesign · Mar 01, 2012 at 10:23 AM · inventorycheckcontain

Inventory Item Check

Hi All,

I've got this inventory from someone else and its working perfect, but now i want to use it in a way that it checks if there are a certain amount of items in it, to open a door, for example a Key (but in some cases i want it to check if there are 4 or 5 items in the inventory. I've spend about 2 hours on it with a friend but we can't seem to get it right.

Inventory script:

 var numberOfSlots=24;
 var numberOfColumns=4;
 var textureSize=64;
 var showBag=true;
 
 public var items : Item[];
 private var windowRect: Rect;
 
 function Start(){
     var slots : float=numberOfSlots;
     var cols : float=numberOfColumns;
     items=new Item[numberOfSlots];
     var rows=Mathf.Ceil(slots / cols);
 
     var width = textureSize * numberOfColumns + 20;
     var height = textureSize * rows + 30;
     windowRect = Rect((Screen.width - width) / 2, (Screen.height - height) / 2, width, height);
 }
 
 function Update(){
     if(Input.GetKeyDown("b")) showBag=!showBag;        
     //logic code here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 }
 
 public function check(oName:String)
 {    
     for(var i = 0; i < items.length; i++)
     {    
         if(items[i])
         {    
             if(items[i].name == oName)
             {
                 return true;
             }else{
                 return false;
             }    
         }    
     }    
 }
 
 function OnGUI(){
     if(showBag){
         GUI.depth = 0;
         CreateWindow();
     }
 }
 
 function DragInventory(windowID:int){
     for(var i=0; i< numberOfSlots; i++){
         var y=Mathf.Floor(i / numberOfColumns);
         var x=i - y * numberOfColumns;
         var rect = Rect(textureSize * x + 10, textureSize * y + 20, textureSize, textureSize);
         var texture : Texture2D;
         if(items[i])texture = items[i].texture;
         var slot = GUI.Button(rect, texture);
         
         if(rect.Contains(Event.current.mousePosition)){
             if(items[i] == null && DragDrop.item != null && Input.GetMouseButtonUp(0)){
                 items[i] = DragDrop.item;
                 //if(items[i].gameObject){
                     //Destroy(items[i].gameObject);
                     //items[i].gameObject=null;
                 //}
             }
                 
             if(Input.GetMouseButtonUp(1) && items[i] && DragDrop.toolSelected){
                 var go=new GameObject.CreatePrimitive(PrimitiveType.Cube);
                 go.renderer.material.mainTexture=items[i].texture;
                 go.AddComponent(Rigidbody);
                 go.AddComponent(AddToInventory);
                 var s=go.GetComponent(AddToInventory);
                 s.item=items[i];
                 items[i] = null;
                 DragDrop.toolSelected=false;
             }
             
             if(slot){}
         }
     }
     GUI.DragWindow();
 }
 
 function AddItem(item : Item){
     for(var i=0; i< numberOfSlots; i++){
         if(!items[i]){
             items[i]=item;
             if(item.gameObject)Destroy(item.gameObject);
             return;
         }
     }
     }
 
 function CreateWindow()
 {
     GUI.BeginGroup(windowRect);
     windowRect = GUI.Window(0, windowRect, DragInventory, "Inventory");
     GUI.EndGroup();
 } 
 

Can anyone help out? There are more scripts in this package, but for as far as my knowledge goes you dont need those, else ask me.

If you are willing to help me out do you want to work with sample items apple, banana, so i can recall everything perfectly.

Thanks in Advance.

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 MrcDesign · Mar 01, 2012 at 10:26 AM 0
Share

Why doesnt the code section work properly :(

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Mar 04, 2012 at 01:32 AM

You should use integers to identify and check the items, it's faster and safer.

Use prints to see what the code is doing, what is in the item array, if the comparison is done properly etc. I'd tell you to use the debugger and breakpoints, but it's making me crazy and I can't get it to work, so the print method is not that bad ^^

Comment
Add comment · Show 5 · 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 MrcDesign · Mar 04, 2012 at 10:08 AM 0
Share

Thanks for the reply, and we was using the Debug.Log to see what piece of code did or did not work properly, but we cant find the right piece of code to actually get into the bag.

We have tried things like

function OnTriggerEnter () [ if (Bag.Items == ("Cube")); Debug.Log ("Cube Found")); ]

But those arent working, so you suggest we should make integers and work with true and false?

avatar image syclamoth · Mar 04, 2012 at 10:24 AM 0
Share

Personally I would stop using Arrays altogether, and swap over to Generic.List ins$$anonymous$$d. $$anonymous$$uch easier, and most of the functionality you need is just there as part of the class.

avatar image MrcDesign · Mar 04, 2012 at 10:41 AM 0
Share

Hmm and that Generic.List is in the documentation? or do i have to google for that

avatar image syclamoth · Mar 04, 2012 at 10:50 AM 0
Share

It's in the .net reference, so you'll have to Google it. Remember that the Unity API isn't all you have available to you- you also have almost all of the .net framework to play with, as well!

avatar image MrcDesign · Mar 04, 2012 at 10:52 AM 0
Share

Oke thanks, well im not really a programmer myself, 3D is my strongest point :P but ill search out how that works.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Check if an array index is empty 1 Answer

How to check if player has an object in inventory 1 Answer

Why does it give me this error and how can i fix it? 1 Answer

how do i display this as GUI 1 Answer

Why wont inventory wont update after or more purchases from in-game shop? 1 Answer


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