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 Bincredible · Aug 14, 2014 at 04:05 PM · bugnot workinginventoryconsole

Inventory problem.

Hey, I am making a platformed game and on the first level there are 6 test tubes that you can pick up, then they will go into the inventory. I made a timer so you can only use something in the inventory along as you are using it at least 1.5 seconds before the last item used. And when you click the item in the inventory it will be removed, what the description says on the item will take effect and it will make a bubble noise also. This all works but sometimes when I click on one of the inventory items it will not make the sound, the description of the item won't happen for example the potion i clicked slows down time, that won't happen. And in the console it will say ArgumentOutOfRangeException: Argument is out of range. I don't know how to fix this any help would be appreciated. Here is my inventory code that is used and an image of the inventory: alt text

 import System.Collections.Generic;
 
 var inventoryBackground : GUISkin;
 var inventoryButton : GUISkin;
 var itemBackground : GUISkin;
 
 var timer : float;
 
 var bubble : AudioSource;
 
 var items : Item[];
 
 var healthGO : GameObject;
 
 var inventoryOpen : boolean = false;
 
 var mainInventory : List.<Item> = new List.<Item>();
 
 function Update(){
 timer += Time.deltaTime;
     if(Input.GetKeyDown(KeyCode.I)){
         inventoryOpen = !inventoryOpen;
     }
 }    
 
 function OnGUI(){
 
 //Open and close te inventory
 GUI.skin = inventoryButton;
 if(GUI.Button(Rect(3,Screen.height-31,100,28),"")){
     inventoryOpen = !inventoryOpen;
 }
 GUI.skin = null;
 //Draw a box around the inventory items
 GUI.skin = inventoryBackground;
 if(inventoryOpen){
     GUI.Box(Rect(2,Screen.height-389,480,355),"");
 }
 GUI.skin = null;
 
     for(var i = 0; i < mainInventory.Count; i++){
     if(inventoryOpen){
      
 /*************** What the buttons do if they are clicked in the inventory ****************/
 GUI.skin = itemBackground;
         if(GUI.Button(Rect(13,Screen.height-327+(48*i),40,40),mainInventory[i].icon)){
         if(timer > 1.5){
         mainInventory.RemoveAt(i);
         timer = 0;
         
             if(mainInventory[i].name == "GreenPotion"){
                 drankGreenPotion();
             }
             if(mainInventory[i].name == "PinkPotion"){
                 drankPinkPotion();
             }
             if(mainInventory[i].name == "OrangePotion"){
                 drankOrangePotion();
             }
             if(mainInventory[i].name == "RedPotion"){    
                 drankRedPotion();
             }
             if(mainInventory[i].name == "PurplePotion"){
                 drankPurplePotion();
             }
             if(mainInventory[i].name == "BluePotion"){
                 drankBluePotion();
             }
             
         }
 }
 GUI.skin = null;
 
 /************** Show the description, name and rarity of the item in the inventory **************/
         GUI.contentColor = Color.black;
         GUI.Label(Rect(55,Screen.height-330+(48*i),500,26),"Name : " + mainInventory[i].name);
         GUI.Label(Rect(55,Screen.height-318+(48*i),500,26),"Description : " + mainInventory[i].description);
         GUI.Label(Rect(55,Screen.height-306+(48*i),500,26),"Rarity : " + mainInventory[i].rarity);
         GUI.contentColor = Color.white;
         
         }
     }
 }
 
 /************ Adding the items to the inventory ************/
 function OnTriggerEnter2D(other : Collider2D){
     if(other.tag == "RoundBluePotion"){
         mainInventory.Add(items[4]);
         Destroy(other.gameObject);
     }
         if(other.tag == "RoundGreenPotion"){
         mainInventory.Add(items[5]);
         Destroy(other.gameObject);
     }
         if(other.tag == "RoundPinkPotion"){
         mainInventory.Add(items[3]);
         Destroy(other.gameObject);
     }
         if(other.tag == "RoundRedPotion"){
         mainInventory.Add(items[1]);
         Destroy(other.gameObject);
     }
         if(other.tag == "RoundPurplePotion"){
         mainInventory.Add(items[2]);
         Destroy(other.gameObject);
     }
         if(other.tag == "RoundOrangePotion"){
         mainInventory.Add(items[0]);
         Destroy(other.gameObject);
     }
 }
 
 function drankOrangePotion(){
 audio.PlayOneShot(bubble.clip);
     var pM = gameObject.GetComponent(PlayerMovement);
                 pM.moveSpeed = 5;
                 yield WaitForSeconds(5);
                 pM.moveSpeed = 2;
 }
 
 function drankBluePotion(){
 audio.PlayOneShot(bubble.clip);
     var mP = gameObject.GetComponent(PlayerMovement);
                 mP.jumpspeed = 7.6;
                 yield WaitForSeconds(5);
                 mP.jumpspeed = 3.8;
 }
 
 function drankRedPotion(){
 audio.PlayOneShot(bubble.clip);
     var ye = healthGO.gameObject.GetComponent(Health);
                 ye.LIVES += 5;
 }
 
 function drankPurplePotion(){
 audio.PlayOneShot(bubble.clip);
     var oy = healthGO.gameObject.GetComponent(Health);
                 oy.LIVES += 10;
 }
 
 function drankPinkPotion(){
 audio.PlayOneShot(bubble.clip);
     var alpha = healthGO.gameObject.GetComponent(Health);
                 alpha.LIVES += 1;
 }
 
 function drankGreenPotion(){
 audio.PlayOneShot(bubble.clip);
 Time.timeScale = 0.5;
 yield WaitForSeconds(10);
 Time.timeScale = 1;
 }

screen shot 2014-08-14 at 16.09.58.png (25.7 kB)
Comment
Add comment · Show 5
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 zaid87 · Aug 15, 2014 at 07:43 AM 0
Share

I'm not really sure on javascript, but anyway, does the console tell which line does the error comes from? It'll be much easier to check.

avatar image lukas_balaz · Aug 15, 2014 at 10:01 AM 0
Share

There must be another script where is Item's definition and where are "name" properties added to items array. Show it.(sorry for my English)

avatar image Bincredible · Aug 15, 2014 at 11:44 AM 0
Share

Here is my other script for the item's definition here:

 class Item {
 
 var name : String;
 var icon : Texture2D;
 var description : String;
 var rarity : Rarity;
 var itemOfClothing : ItemOfClothing;
 
 }
 
 enum Rarity {
 
 common,
 rare,
 awesome
 
 }
 
 enum ItemOfClothing {
 
 shoes,
 trousers,
 shirt,
 hat,
 notClothingItem
 
 }
avatar image Bincredible · Aug 15, 2014 at 11:45 AM 0
Share

And the error in the console says this:

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[Item].get_Item (Int32 index) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) Inventory.OnGUI () (at Assets/Inventory/Inventory.js:51)

avatar image Bincredible · Aug 15, 2014 at 11:46 AM 0
Share

I'm guessing that's line 51 but I can't find anything wrong with it...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Landern · Aug 15, 2014 at 12:15 PM

You're removing items then evaluating the same index position in the list:

  mainInventory.RemoveAt(i);
  timer = 0;
 
  if(mainInventory[i].name == "GreenPotion"){
     drankGreenPotion();
  }

Now, the variable i was already set above the the code above in the for loop. If you for instance remove the last item in the list and then it hits your if statements, guess what, the item doesn't exist, your trying to access the removed item/element in the List.

Example: If you had 10 items, you go with the last item, the variable i is set to 9, you remove 9, then you try and gain access to a property/field called name... FAIL, your index is out of range for the list.

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 Bincredible · Aug 15, 2014 at 12:37 PM 0
Share

So would I need to not use the if statement which checks the name then?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

OnGUI mystically stopped rendering buttons and text. 1 Answer

Inup.GetButtonDown not working? 1 Answer

Why is my script not working like it should 3 Answers

Duplicated object and original behave differently 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