- Home /
How does a simple inventory work?
I need a two slot inventory in my game but I'm not that sure about how I am supposed to do it. I basically have 2 empty variables named slot1 and slot2. The plan is that if you press E while you're in the area of the gameObject (the area is just an invisible isTrigger sphere), it gets added to your inventory as slot1. But I am not that sure how I would make that adding work. Here is my script that I wrote (I haven't made anything for slot2 yet on purpose):
var slot1 : Transform;
var slot2 : Transform;
function Update () {
}
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Enemy") {
slot1 = other.gameObject.Transform;
}
}
function OnTriggerExit (other : Collider) {
if (other.gameObject.transform == slot1) {
slot1 = null;
}
}
Please explain how your editing of my script works so i can improve my scripting in the future.
PS: Please write the script in javascript.
Answer by BossGrand · Oct 28, 2013 at 01:48 AM
So for your gameobject that is the "weapon" you'll want a script on there that has functions for dealing with firing input.
Then you should grab that script when you collide with the weapon using this http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
I would have your update check to see if you have this "weapon" and if it does then you allow the input check for firing that weapon and if it is down then you fire.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120745im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Getting started: Inventory script 0 Answers
Adding Muiltipule GUI.Buttons In an Inventory System Based On Arrays 1 Answer
Trying to make a simple inventory: 0 Answers
Inventory code not working [JS] 0 Answers
Crafting system 2 Answers