- Home /
Question by
JDM Industrys · Feb 01, 2013 at 08:43 PM ·
itemflashlightpick-up
Help with picking up an item(flashlight)
Hi my friends an i are new to unity and were worknig on a game and one of the parts of the game is to pick up the flashlight and the game starts and yeah im new to coding as well and i was wonding if anyone could write or send me the code to do so because im am the coder in the game and yeah i know its kinda cheap to use someone elses code but im kinda shot on time for this part of the game many thanks ~SkIlZzz
Comment
Answer by iwaldrop · Feb 01, 2013 at 10:01 PM
Nobody here is going to write your code for you. But the following should give you a basic idea of what to do.
Item item;
void Update()
{
if(item != null && Input.GetButtonDown("pickup"))
{
// pickup your item, add it to inventory, or disable the item's gameobject...
// whatever you do when you pick something up
}
}
void OnTriggerEnter(Collider other)
{
Item i = other.GetComponent(typeof(Item));
if (i != null)
item = i;
}
void OnTriggerExit(Collider other)
{
Item i = other.GetComponent(typeof(Item));
if (item == i)
item = null;
}