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 /
  • Help Room /
avatar image
0
Question by Tompis1995 · Apr 13 at 11:25 PM · uilistinventory systemgamepad

How can I shift index numbers in a decremental manner?

I have a flexible list that can have objects added to or removed from at any given time. Right now, I'm trying to update the index numbers whenever any object is removed. To put it specifically, I want the index numbers of all items with a greater index value than the removed item to decrement by 1. After that, I want the maximum Index value to update and reflect the changes to the inventory. However, I'm having an extremely difficult time getting it to work because I have the mechanism set up in a way that controls the inventory via a gamepad instead of the mouse. The issue is that all the attempts I have pulled would either make the succeeding item of the selected one disappear with it or remove all succeeding items. Often times, it would take two taps on the gamepad to select the next item. Can someone help me with this? Thanks! Relevant Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
 public class InventoryManager : MonoBehaviour
 {
     public int index;
     public int maxIndex;
     [SerializeField] RectTransform contentTransform;
     [SerializeField] bool buttonDown;
     [Header("Inventory Info")]
     public PlayerInventory playerInventory;
     [SerializeField] private GameObject blankInventorySlot;
     [SerializeField] private GameObject inventoryPanel;
     [SerializeField] private TextMeshProUGUI descriptionText;
     public bool isPressRight, isPressLeft, isPressConfirm;
     public int HorizontalMovement;
     PlayerControls controls;
     bool useButtonDown;
     public InventoryItem currentItem;
     public bool inventoryActive;
     public Animator animInven;
 
     public void Awake()
     {
         controls = new PlayerControls();
         controls.Gameplay.UseItem.performed += ctx => onPressConfirm();
         controls.Gameplay.UseItem.canceled += ctx => onReleaseConfirm();
         controls.Gameplay.BringUpInventory.performed += ctx => inventoryActive = true;
         controls.Gameplay.CloseInventory.performed += ctx => inventoryActive = false;
     }
 
     public void SetTextAndSelectionStatus(string description, bool usable, InventoryItem newItem)
     {
         currentItem = newItem;
         descriptionText.text = description;
 
     }
     void MakeInventorySlots()
     {
         if (playerInventory)
         {
             int inventIndex = 0;
             foreach (InventoryItem item in playerInventory.myInventory)
             {
                 if(blankInventorySlot != null && playerInventory.myInventory[inventIndex].numberHeld > 0)
                 {
                     InventorySlot newSlot = Instantiate(blankInventorySlot, transform.position, transform.rotation).GetComponent<InventorySlot>();
                     newSlot.transform.SetParent(inventoryPanel.transform);
                     newSlot.Setup(playerInventory.myInventory[inventIndex], this, inventIndex);
                     inventIndex++;
                 }
                 else
                 {
                     playerInventory.myInventory.Remove(playerInventory.myInventory[inventIndex]);
                 }
             }
             inventIndex = maxIndex;
         }
     }
     // Start is called before the first frame update
     void Start()
     {
         MakeInventorySlots();
         isPressRight = isPressLeft = false;
         useButtonDown = false;
     }
 
 
     public void OnPressRight()
     {
         isPressRight = true;
     }
     public void onReleaseRight()
     {
         isPressRight = false;
     }
     public void onPressLeft()
     {
         isPressLeft = true;
     }
     public void onReleaseLeft()
     {
         isPressLeft = false;
     }
     public void onPressConfirm()
     {
         if (!useButtonDown)
         {
             if (currentItem)
             {
                 currentItem.Use();
                 isPressConfirm = true;
                 ClearInventorySlots();
                 MakeInventorySlots();
             }
         }
     }
     public void onReleaseConfirm()
     {
         isPressConfirm = false;
     }
 
     void ClearInventorySlots()
     {
         for(int i = 0; i < inventoryPanel.transform.childCount; i++)
         {
             Destroy(inventoryPanel.transform.GetChild(i).gameObject);
         }
     }
 
     // Update is called once per frame
 
     void Update()
     {
         maxIndex = playerInventory.myInventory.Count - 1;
         animInven.SetBool("active", inventoryActive);
         if (Input.GetAxis("DPad") != 0 && !isPressConfirm)
         {
 
             if (!buttonDown)
             {
 
               
                 if (Input.GetAxis("DPad") > 0)
                 {
                     if (index < maxIndex)
                     {
                         index++;
 
                         if (index > 3 && index < maxIndex - 2)
                         {
                             contentTransform.offsetMin -= new Vector2(58, 0);
                         }
 
                     }
                     else
                     {
                         index = 0;
                         contentTransform.offsetMin = new Vector2(-400, -58);
                     }
                 }
                 else if (Input.GetAxis("DPad") < 0)
                 {
                     if (index > 0)
                     {
                         index--;
                         if (index < maxIndex - 3 && index > 2)
                         {
                             contentTransform.offsetMin -= new Vector2(-58, 0);
                         }
                     }
                     else
                     {
                         index = maxIndex;
                         contentTransform.offsetMin = new Vector2(-400 - ((maxIndex - 6) * 58), -58);
                     }
                 }
                 buttonDown = true;
             }
         }
         else
         {
             buttonDown = false;
         }
     }
     private void OnEnable()
     {
         controls.Gameplay.Enable();
     }
 
     private void OnDisable()
     {
         controls.Gameplay.Disable();
     }
 
     
 }
 

For the sake of space, I will leave out other relevant codes unless requested. Thanks again!

Comment
Add comment
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

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

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

Need help with 3d inventory system 0 Answers

Display list as UI or GUI 0 Answers

Help Regarding Making A Server List by Instantiating prefabs 0 Answers

Prefab not grabbing correct Rect Transform Values (Not working) 0 Answers

Why is drag and drop not working? 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