Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheChanger · Apr 24, 2019 at 06:47 PM · colliderinventoryweaponfor-looppickup

Inventory adds an item only once.

I have been following a youtube channel called Sykoo on how to make an inventory in unity and when I tested the code that I had written it seemed to work (although the game object item was added to both of my raw images) and the item was added to my inventory. After I exited the game and returned to it again it did not add to my inventory. Any help would be great thank you.Here is the code (if ive made a glaring mistake I may not have noticed it as Im a beginner to unity and sorry there is 3 scripts that make my inventory so there is a lot of code):

     void Start()
     {
         inputDown = 0;
         slots = slotHolder.transform.childCount;
         slot = new Transform[slots];//Limit of slots //Slot is a new arra. Going to be amount of slots there are.
         DetectInventorySlots();
     }
   

     public void OnTriggerEnter(Collider other)
     {
         if (other.GetComponent<Item>())
         {
             itemPickedUp = other.gameObject;
             AddItem(itemPickedUp);
             
         }
     }
     public void OnTriggerExit(Collider other)
     {
         if(other.GetComponent<Item>()) {
             
             itemAdded = false;
             
 
         }
     }
     public void AddItem(GameObject item)
     {
         for (int i =0; i < slots; i++) //Check for each slot. this is a regular for loop that is created.
         {
             if (slot[i].GetComponent<Slot>().empty==false && itemAdded==false)//If the slot is empty
             {
                 slot[i].GetComponent<Slot>().item = itemPickedUp;
                 slot[i].GetComponent<Slot>().itemIcon = itemPickedUp.GetComponent<Item>().icon;
                 itemAdded = true;
             }
 
         }
     }
     public void DetectInventorySlots()
     {
         for(int i = 0; i<slots; i++)//check for loop with integer called i at start. while i is lower then slots going to increase i by 1
         {
             slot[i] = slotHolder.transform.GetChild(i);//
             print(slot[i].name);//print amount of slots
         }
     }
 }
 The second "Slot" script:
         void Update()
         {
             if (item && itemIcon)
             {
                 empty = false;//If there is an item empty is going to be false
                 itemIcon = item.GetComponent<Item>().icon; //the items icon is equal to the icon of that gameobject
                 this.GetComponent<RawImage>().texture = itemIcon; //The slots texture is equal to itemIcon (which is a texture)
             }
             else
             {
                 empty=true;
             }
             
         }
 
     
     }
 

The last script that is applied to the weapon (Item):

     public string type; //Type of item
     public int ID;
     public string description;
     public Texture icon;
     public bool pickedUp;
    
   
Comment
Add comment · Show 4
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 jmgek · Apr 24, 2019 at 07:29 PM 0
Share

Not many people want to wade through 200+ lines of code, try and trim it down to the lines you think to be the problem so we can help you quicker.

avatar image TheChanger jmgek · Apr 24, 2019 at 08:02 PM 0
Share

I hope I trimmed enough here. Sorry about that. If theres anything that needs to be added back in im more than happy to do that.

avatar image chaitae · Apr 26, 2019 at 10:46 PM 0
Share

In leaving your game and reentering your game do you mean changing scene or pressing the play and stop button? If so, you need to save your inventory data.

avatar image TheChanger chaitae · Apr 27, 2019 at 11:19 AM 0
Share

When i press the play and stop button. As in I play it once then it works and adds to my inventory then I press stop then I press play again and it doesnt add to my inventory.

0 Replies

· Add your reply
  • Sort: 

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

140 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 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 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 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 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 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 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 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 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

How can I detect if box is touching a tag named weapon? 1 Answer

PickUp Weapon in Brick Breaker Game 1 Answer

Player Animation change when pickup Weapon 0 Answers

Pick up gameobject and go to inventory, 1 Answer

Switching GameObject 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