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 topkekker · Oct 25, 2014 at 08:15 PM · c#gameobjectinventory

Creating multiple objects?

Im basically trying to get a inventory to work when the player uses the 1-2-3 keys. Heres what i got so far:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class playerInventory : MonoBehaviour {
     public int currentWeapon;
     public GameObject test;
     public int selected = 0;
     public bool equipped = false;
     
     
     List<GameObject> inventory = new List<GameObject>();
     List<GameObject> prevInventory = new List<GameObject>();
     
     // Use this for initialization
     void Start () {
         
         inventory.Add(test);
         Debug.Log (inventory.Count);
         
         
         
     }
     
     // Update is called once per frame
     void Update () {
         loopThroughBackpack ();
     }
     
     void loopThroughBackpack(){
         if (Input.GetKeyDown (KeyCode.Alpha1)) {
             selected = 0;
             GameObject item = Instantiate(inventory[selected], transform.parent.position, transform.parent.rotation) as GameObject;
             if(equipped == true){
 
                 item.renderer.enabled = false;
                 prevInventory.Add(test);
                 Debug.Log("Item unequipped");
                 equipped = false;
                 Debug.Log(equipped);
             }else if(equipped == false){
 
                 Debug.Log("Object equipped");
 
                 item.renderer.enabled = true;
                 item.transform.parent = gameObject.transform;
                 item.transform.localPosition = new Vector3(0.6f,-0.5f,1);
                 item.transform.localRotation = Quaternion.identity;
                 equipped = true;
                 Debug.Log(equipped);
             }
             
             
             
             
             
         }
         else if(Input.GetKeyDown(KeyCode.Alpha2)){
             selected = 1;
             
             
         }
         
         
         
     }        
 }
 
 

Now I've asked multiple questions around the same topic but i have never been clear enough.

Anyways my problem is this: Once the player hits 1, It creates a item, then when he hits 1 again to dequip it, it doesnt hide it. it just spawns a testcanteen somewhere on the map then creates even more on the players hand.

Also if anyone is feeling kind enough, Could they please tell me if the way im trying to achieve a inventory is correct or not?

Thanks a tonne.

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 Xetros · Oct 25, 2014 at 08:24 PM 0
Share

I almost completed my Inventory system in Javascript, if you want i can post some code here so you can take a look at it. I think you are going in the right direction.

PS i am not very good in c#

avatar image topkekker · Oct 25, 2014 at 08:31 PM 0
Share

That would be nice to see.

avatar image Xetros · Oct 25, 2014 at 08:37 PM 0
Share

Alright well, first of all i am using an itemhandler script which basiclly holds all the properties of an item. This is the code for it:

 #pragma strict
     
     
     public class ItemHandler
     {
         //ID
         var itemID : int;
         //Name
         var itemName : String;
         //Icon
         var itemIcon : Texture2D;
         //Desc
         var itemDesc : String;
         //Type
         var itemType : ItemType;
         //equipped
         var itemEquipped : boolean;
     }
     
     
     public enum ItemType
     {
         Weapon,
         Consumable,
         CraftingComponent,
         $$anonymous$$iscItem,
     }
     
     
     function ItemHandler(id : int, name : String, desc : String, type : ItemType, equipped : boolean)
     {
         this.itemID = id;
         this.itemName = name;
         this.itemIcon = Resources.Load("Icons/" + name);
         this.itemDesc = desc;
         this.itemType = type;
         this.itemEquipped = equipped;
         
     }
     
     
     function ItemHandler()
     {
         itemID = -1;
     }
 

Then i also have an itemDatabase script which basiclly holds the items:

 #pragma strict
 
 import System.Collections.Generic;
 
 
 var itemList : List.<ItemHandler> = new List.<ItemHandler>();
 
 function Start()
 {
     
     
 //            Standaard            ID        Naam        Beschrijving            Type        Aan
     itemList.Add(new ItemHandler(1, "Pistol_9mm", "It's a 9mm pistol!", ItemType.Weapon, false ));
     itemList.Add(new ItemHandler(2, "Test_Item", "Testing the items!", ItemType.$$anonymous$$iscItem, false));
     
 }

And finally i have an Inventory Script which draws a grid and show a specific item in one box. That script is to long to post here but it is using Lists just like yours but ins$$anonymous$$d of passing the Lists a GameObject, i pass it ItemHandler, just like i did in the database.

I hope this helped atleast a little bit ;)

PS I removed most of the comments because they are not in English

avatar image topkekker · Oct 25, 2014 at 08:46 PM 0
Share

Cheers for this, ll try my best to understand it all

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

29 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Drag and Drop icons into slots 1 Answer

Passing gameObjects to Another Script: NullReference Exception 1 Answer

Problem with taking a gameobject from gameobject list to inventory. 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