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 Conect11 · Oct 26, 2013 at 04:21 AM · arraystringinventoryinteger

[SOLVED] First array slot blocking second array slot

Hello everyone,

Am having a weird little issue. In my pause menu I want to display the number of items the character has, but if the number is 0, then I want the item to not appear. so if "Health Potion = 1" 1 health potion would appear in the inventory, but if health potion = 0, then the line disappears. Same goes for bread, (hard tack) water, etc. Things were actually working until I added a second item. (first, if you want to get technical, array slot 1 worked fine, did that first, then went back and added array slot 0. The problem is that array slot 1 seems to be tied to slot 0 somehow, meaning that I can have 10,000 hard tack crackers, but they won't appear in my inventory guitext until I pick up 1 health potion. (array slot 0) Further, once I've picked up a health potion, I can never use the last one, meaning that the player needs 2 or more at all times, and if they have 1, (or less) then they can't use anything out of slots 0 or 1. I figure it's an issue with line placement in the code, but thought I'd throw it out to the community while I bang my head against the keyboard. Thanks, and God bless.

 //inventory
 static var inventoryArray : int[] = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
 var inventoryText : GameObject;
 var inventoryText2 : GameObject;
 var inventoryText3 : GameObject;
 var inventoryText4 : GameObject;
 static var PrisonKey : int = 0;
 static var TempleKey : int = 0;
 static var HealthPotion : int = 0;
 static var HardTack : int = 0;
 static var Water : int = 0;
 static var AppleBrew : int = 0;
 //inventoryText.guiText.text = "Health Potion " + "[" + inventoryArray[0] + "]" + "\n" + "Hard Tack " + "[" + inventoryArray[1] + "]" + "\n" + "Water " + "[" + inventoryArray[2] + "]" + "\n" + "Apple Brew " + "[" + inventoryArray[3] + "]" + "\n";
 function Start () {
 
 inventoryText.guiText.enabled = false;
 inventoryText2.guiText.enabled = false;
 inventoryText3.guiText.enabled = false;
 inventoryText4.guiText.enabled = false;
 }
 
 function Update () {
 
 
 
 if(inventoryArray[0] > 0) {
 inventoryText.guiText.enabled = true;
 inventoryText.guiText.text = "Health Potion " + "[" + inventoryArray[0] + "]" + "\n";
 if(inventoryArray[0] > 0) {
 if(Input.GetButtonDown("Stuff"))
 healthPotion();
 }
 
 if(inventoryArray[1] > 0) {
 inventoryText2.guiText.enabled = true;
 inventoryText2.guiText.text = "Hard Tack " + "[" + inventoryArray[1] + "]" + "\n";
 if(inventoryArray[1] > 0) {
 if(Input.GetButtonDown("Sword Slash"))
 hardTack();
 }
 if(Input.GetButtonDown("Jump"))
 
 if(Water > 0) {
 
 water();
 }
 }
 }
 }
 
 function healthPotion ()  {
 
 Playerhealth.curHealth += 15;
 inventoryArray[0] -=1;
 HealthPotion -=1;
 }
 
 function hardTack ()  {
 
 Playerhunger.curHunger -= 5;
 inventoryArray[1] -=1;
 HardTack -=1;
 
 if(inventoryArray[1] == 0) {
 inventoryText2.guiText.enabled = false;
 }
 }
 
 function water ()  {
 
 Playerthirst.curThirst -= 100;
 Water -=1;
 }


Item Pick up Script (Health Potion)

 var    chestSound : AudioClip; 
 var treasureChest : GameObject; 
  
 function OnTriggerEnter (col : Collider) {
  
 if(col.gameObject.tag == "Player") { 
 Inventory.inventoryArray[0]++;
 Inventory.HealthPotion++;
 AudioSource.PlayClipAtPoint(chestSound, transform.position); 
 treasureChest.animation.Play(); 
 Destroy(gameObject);
  
  
 }
 }


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 Eric5h5 · Oct 26, 2013 at 05:04 AM 0
Share

not using arrays

You need to use arrays (or some sort of collection); it's not really an option, if you want your coding to be even remotely sane.

avatar image Conect11 · Oct 26, 2013 at 05:08 AM 0
Share

ok, have went the array route, but now with a new wrinkle tripping me up.

1 Reply

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

Answer by fafase · Oct 26, 2013 at 11:51 PM

Your code is not so well indented but I feel your second check, the one for inventory[1] is within the first which makes sense why you need one potion to see your crackers.

The reason why you cannot use the last one in your inventory is because you use:

  if(inventoryArray[0] > 0) 

and the Input is inside of that. To use down to 0 you need:

 if(inventoryArray[0] >= 0) 

By the way since you have all items as variables, there is no real need for the array. You should pick one or the other.

Comment
Add comment · Show 9 · 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 Conect11 · Oct 26, 2013 at 11:57 PM 0
Share

Yeah, I could definitely do better at indenting. As I'm learning code I haven't yet gotten to just doing that. Is there a rhyme or reason, or is it for ease of reading? Thanks on the >=, that makes sense. ah, didn't realize 1 was inside 0's array. Newb here, just trying to make my way :)

As for the array / static vars. I agree. I would love to do it one or the other. The reason I did it this way is actually Playerprefs. Had the hardest time trying to figure out how to save from an array, even tried using something called "PlayerprefsX" and it never worked, though I could save static vars, so did both; the array for displaying not only the Name of the product, but also the quantity, and the static var so the items could be carried over between game sessions.

avatar image fafase · Oct 27, 2013 at 12:06 AM 0
Share

In C# and most languages, the indentation just helps you read. See in your case, indentation would have told me right away that the second if is nested.

In some languages like Python and Boo, indentation is the code. There is no {} and you use indentation ins$$anonymous$$d

 if(something):
     Action();
avatar image Conect11 · Oct 27, 2013 at 12:14 AM 0
Share

ok, I have a stupid question. Really stupid, newbish question. How do I unnest the hard tack call, do I take it completely out of update and make it an entirely new function?

avatar image fafase · Oct 27, 2013 at 12:20 AM 0
Share

Nested:

 if(something){
    if(somethingElse)
    {}
 }

Not nested:

 if(something){
 } 
 if(somethingElse){
 }

   

It is all about closing the matching braces

avatar image fafase · Oct 27, 2013 at 01:43 AM 1
Share

Ok here is what you should have:

 function Update () {
    if(inventoryArray[0] > 0) {
       inventoryText.guiText.enabled = true;
       inventoryText.guiText.text = "Health Potion " + "[" + inventoryArray[0] + "]" + "\n";
       if(Input.GetButtonDown("Stuff"))
          healthPotion();
    }
    if(inventoryArray[1] > 0) 
    {
       inventoryText2.guiText.enabled = true;
       inventoryText2.guiText.text = "Hard Tack " + "[" + inventoryArray[1] + "]" + "\n";
       if(Input.GetButtonDown("Sword Slash"))
          hardTack();
       if(Input.GetButtonDown("Jump"))
          if(Water > 0) 
          {
              water();
          }
      }
 }

But here is also what you are missing:

 function healthPotion ()  
 {
    Playerhealth.curHealth += 15;
    inventoryArray[0] -=1;
    HealthPotion -=1;
    inventoryText.guiText.enabled = false; //Here
 }
Show more comments

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

16 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

Related Questions

Add GameObject to GameObjectArray[] 3 Answers

Why can't I save an array? 1 Answer

Setting generic list length 2 Answers

How to create an array of Game Objects with different tags. 1 Answer

Hierarchy index of prefab change when putting in into an array? 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