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 /
  • Help Room /
avatar image
0
Question by Kybo10 · Jan 28, 2017 at 04:43 AM · c#arraylistinventory system

Need help with 3d inventory system

Hey guys, I have been messing around with a 3d RPG type game, and I have had trouble with an inventory type system. I basically want 3 weapons on each side, representing each hand. You can select each one using arrow keys, left with scroll through left side, right will scroll through right side.

If there is nothing there, and it instantiate a fist collider instead. What is happening though, is I have 6 slots for the inventory, 0-2 is left side and 3-5 is the right side, but when I have a weapon in my first slot, [0], I debug.log the name, and it recognizes that its at inventory[0], but when I see if its selected, it waits until i scroll to slot where [1] is supposed to be, the second slot, then it applies it. So it seems to be that when the first slot is selected, it recognizes it as inventory [1], I dont know how this is possible lol. Here is my code, maybe I have an easy mistake? inventory is a list, but I turn it into an array so that the player can choose which slot to put the weapon they pick up. I heard this is easier incase I want to do like a bag system, so I can just do inventory.add.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Inventory : MonoBehaviour {
 
     public List<GameObject> inventory = new List<GameObject>();
     public Image[] slots;
 
     Player player;
     GameObject leftEquipped;
 
     private void Awake()
     {
         slots[0].transform.parent.transform.FindChild("Selected").gameObject.SetActive(true);
         slots[3].transform.parent.transform.FindChild("Selected").gameObject.SetActive(true);
         player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
     }
 
     private void FixedUpdate()
     {
         for (int i = 0; i < inventory.Count; i++)
         {
             if (inventory[i] != null)
             {
                 slots[i].sprite = inventory[i].GetComponent<Item>().itemIcon;
                 slots[i].color = Color.white;
             }
         }
         Debug.Log(inventory[0].name);
 
     }
 
     private void Update()
     {
         SelectItem();
         //UpdateInventory();
     }
 
     public void InventoryAdd(int input, string name, int id, Sprite icon, GameObject go)
     {
         GameObject[] i = inventory.ToArray();
         int arrayNum = input - 1;
         i[arrayNum] = (GameObject)Resources.Load("Weapons/" + name);
         slots[arrayNum].sprite = icon;
         inventory = new List<GameObject>(i);
         bool left = input >= 1 && input <= 3;
         EquipWeapon(inventory[input].name, left);
     }
 
     //void UpdateInventory()
     //{
     //    for (int i = 0; i < inventory.Count; i++)
     //    {
     //        if (inventory[i] == null)
     //        {
     //            Image image = slots[i];
     //            Color newColor = image.color;
 
     //            image = null;
 
     //            newColor.a = 0;
     //            image.color = newColor;
 
     //        }
     //        else
     //            return;
 
 
     //    }
     //}
 
     void SelectItem()
     {
         bool leftArrow = Input.GetKeyDown(KeyCode.LeftArrow);
         bool rightArrow = Input.GetKeyDown(KeyCode.RightArrow);
 
         if (leftArrow)
         {
             GameObject slot1 = slots[0].transform.parent.transform.FindChild("Selected").gameObject;
             GameObject slot2 = slots[1].transform.parent.transform.FindChild("Selected").gameObject;
             GameObject slot3 = slots[2].transform.parent.transform.FindChild("Selected").gameObject;
             bool slot1Active = slot1.activeSelf;
             bool slot2Active = slot2.activeSelf;
             bool slot3Active = slot3.activeSelf;
 
             if (slot1Active)
             {
                 Debug.Log("OWOWOWOWOWO");
                 slot2.SetActive(true);
                 slot1.SetActive(false);
                 if (inventory[0] != null)
                     EquipWeapon(inventory[0].name, true);
                 else
                     EquipFist(true);
             }
             if (slot2Active)
             {
                 slot3.SetActive(true);
                 slot2.SetActive(false);
                 if (inventory[1] != null)
                     EquipWeapon(inventory[1].name, true);
                 else
                     EquipFist(true);
             }
             if (slot3Active)
             {
                 slot1.SetActive(true);
                 slot3.SetActive(false);
                 if (inventory[1] != null)
                     EquipWeapon(inventory[1].name, true);
                 else
                     EquipFist(true);
             }
         }
 
         else if (rightArrow)
         {
             GameObject slot4 = slots[3].transform.parent.transform.FindChild("Selected").gameObject;
             GameObject slot5 = slots[4].transform.parent.transform.FindChild("Selected").gameObject;
             GameObject slot6 = slots[5].transform.parent.transform.FindChild("Selected").gameObject;
             bool slot4Active = slot4.activeSelf;
             bool slot5Active = slot5.activeSelf;
             bool slot6Active = slot6.activeSelf;
 
             if (slot4Active)
             {
                 slot5.SetActive(true);
                 slot4.SetActive(false);
                 if (inventory[4] != null)
                     EquipWeapon(inventory[4].name, false);
                 else
                     EquipFist(false);
             }
             if (slot5Active)
             {
                 slot6.SetActive(true);
                 slot5.SetActive(false);
                 if (inventory[5] != null)
                     EquipWeapon(inventory[5].name, false);
                 else
                     EquipFist(false);
             }
             if (slot6Active)
             {
                 slot4.SetActive(true);
                 slot6.SetActive(false);
                 if (inventory[6] != null)
                     EquipWeapon(inventory[6].name, false);
                 else
                     EquipFist(false);
             }
         }
     }
 
     void EquipWeapon(string name, bool left)
     {
         if (left)
         {
             Destroy(player.leftHand.transform.GetComponentInChildren<HitCollider>().gameObject);
 
             leftEquipped = (GameObject)Instantiate(Resources.Load("Weapons/" + name), player.leftHand.transform, false);
             leftEquipped.GetComponentInChildren<Canvas>().gameObject.SetActive(false);
         }
         else
         {
             Destroy(player.rightHand.transform.GetComponentInChildren<HitCollider>().gameObject);
 
             leftEquipped = (GameObject)Instantiate(Resources.Load("Weapons/" + name), player.rightHand.transform, false);
             leftEquipped.GetComponentInChildren<Canvas>().gameObject.SetActive(false);
         }
     }
     void EquipFist(bool left)
     {
         if (left)
         {
             Destroy(player.leftHand.transform.GetComponentInChildren<HitCollider>().gameObject);
             leftEquipped = (GameObject)Instantiate(Resources.Load("Weapons/Fist"), player.leftHand.transform, false);
         }
         else
         {
             Destroy(player.rightHand.transform.GetComponentInChildren<HitCollider>().gameObject);
             leftEquipped = (GameObject)Instantiate(Resources.Load("Weapons/Fist"), player.rightHand.transform, false);
         }
     }
 }
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

311 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Cant figure out how to implement a inventory system 1 Answer

How to get all children of a Gameobject with a certain component 2 Answers

Array with pushing values? 0 Answers

Array (List) with multiple variable types? 2 Answers

Assign role randomly from array for my online game 2 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