- Home /
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.
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#
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