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 OctoSloths · Nov 20, 2014 at 06:04 AM · javascriptarrayequip

Array in inventory, adding and greater than

So I'm trying to make a simple inventory and I want the player to be able to hit "h" to equip an object if the array is greater than 1, and then subtract 1 from the array so the inventory reads that there are zero lanterns in inventory and then /not/ be able to hit "h" and equip the object again since there are no lanterns in the inventory. Here is my script:

 //inventory
 static var inventoryArray : int[] = [0,0,0,0,0];
 var inventoryText : GameObject;
 var firstPersonController:Transform;
 var lanturn:GameObject;
 function Update () {
  
         inventoryText.guiText.text = "Lantern " + "[" + inventoryArray[0] + "]";
         //inventoryArray[0]++;
         //inventoryArray[1] += 2;
         
      if (inventoryArray.length >= 1){
         if (Input.GetKeyDown("h")){
             var go = Instantiate(lanturn) as GameObject;
             go.transform.parent = firstPersonController;
             print("lemon");
             inventoryArray - 1;
         }
 }
 }
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 Sir-Irk · Nov 20, 2014 at 06:40 AM 0
Share

If you are looking to resize a collection often then you'll find it easier to use a List(Note this is C# but you can use List in UnityScript with slightly different syntax). Also look at at Eric5h5's answer on this page.

If you are just keeping track of how many lanterns you have with position 0 of your array then go with Uldeim's answer.

1 Reply

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

Answer by Uldeim · Nov 20, 2014 at 07:09 AM

I'm going to go out on a limb here and make some guesses, because your code is very confusing.

Assumption: You want the number of lanterns to be stored at array position 0. That is, inventoryArray = [5,0,0,0] means you have 5 lanterns currently in your inventory.

In this case, you don't need to check the array length.

You just need:

      if (Input.GetKeyDown("h") && inventoryArray[0] > 0){
          var go = Instantiate(lanturn) as GameObject;
          go.transform.parent = firstPersonController;
          print("lemon");

          inventoryArray[0] -= 1;
      }


I would also correct your spelling of "lantern", since incorrect spellings can really bite you long term.

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 OctoSloths · Nov 20, 2014 at 03:07 PM 0
Share

Thank you so much, I'm sorry for the confusing script and I've also changed the spelling errors.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Reading and Storing External Data into Memory (From Text) 1 Answer

Type 'Object' does not support slicing. 1 Answer

Javascript - Calling a random String 2 Answers

Index out of bounds even tho it shouldnt be [Javascript] 1 Answer

Need help with OnTriggerEnter 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