Здравствуйте, такая проблема, добавил в свой инвентарь Аптечки "Health" и патроны "Ammo"... С аптекой все норм, а вот патроны по факту заряжаются, только если выбросить оружие и поднять снова.
Цитата
public class Inventory : MonoBehaviour
{
List<Item> list;
public GameObject inventory;
public GameObject container;
public MonoBehaviour fps;
public FPSHandR controller2;
public FPSHandL controller;
public Text txt;
public Weapon wp;
int random_ammo;
public Heath hp;
// Use this for initialization
void Start () {
list = new List<Item>();
Cursor.visible=false;
controller2 = GetComponent<FPSHandR> ();
controller = GetComponent<FPSHandL> ();
random_ammo = Random.Range (5, 10);
hp = GetComponent<Heath> ();
wp = GetComponent <Weapon> ();
}
// Update is called once per frame
void Update () {
txt.text = "";
RaycastHit hit;
if(Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, 2.5f)){
if (hit.collider.tag == "Loot"){
txt.text = "Взять:E";
if(Input.GetKeyUp(KeyCode.E))
{
Item item = hit.collider.GetComponent<Item>();
if(item != null)
{
list.Add(item);
Destroy(hit.collider.gameObject);
txt.text = "";
}
}
}
}
if(Input.GetKeyUp(KeyCode.B))
{
if(inventory.activeSelf)
{
fps.enabled = true;
inventory.SetActive(false);
Cursor.visible=false;
for(int i = 0; i < inventory.transform.childCount; i++)
{
if(inventory.transform.GetChild(i).transform.childCount > 0)
{
Destroy(inventory.transform.GetChild(i).transform.GetChild(0).gameObject);
}
}
}
else
{
fps.enabled = false;
inventory.SetActive(true);
Cursor.visible=true;
int count = list.Count;
for(int i = 0; i < count; i++)
{
Item it = list[i];
if(inventory.transform.childCount > i)
{
GameObject img = Instantiate<GameObject>(container);
img.transform.SetParent(inventory.transform.GetChild(i).transform);
img.GetComponent<Image>().sprite = Resources.Load<Sprite>(it.sprite);
img.GetComponent<Drag>().item = it;
}
else break;
}
}
}
}
void use(Drag drag)
{
if (drag.item.type == "LeftHand") {
HandItem myitem = Instantiate<GameObject> (Resources.Load<GameObject> (drag.item.prefab)).GetComponent<HandItem> ();
controller.addHand (myitem);
}
if (drag.item.type == "Ammo") {
wp.ammo += random_ammo;
}
if (drag.item.type == "Health") {
hp.LP += random_ammo;
} else if (drag.item.type == "RightHand") {
HandItem myitem = Instantiate<GameObject> (Resources.Load<GameObject> (drag.item.prefab)).GetComponent<HandItem> ();
controller2.addHand (myitem);
}
list.Remove (drag.item);
Destroy(drag.gameObject);
}
void remove(Drag drag)
{
Item it = drag.item;
GameObject newo = Instantiate<GameObject>(Resources.Load<GameObject>(it.prefab));
newo.transform.position = transform.position + transform.forward + transform.up;
Destroy(drag.gameObject);
list.Remove(it);
}
}