void OnTriggerEnter2D(Collider2D other) { string colliderName = other.gameObject.name; //when colliding with the player if (colliderName == "Player" && timeSincePickup > 1f) { //if the item is to be bought and the player has enough gold to do so, of if the item is not sold if ((isItemSold && PlayerScript.MyInstance.GetComponent <CurrenciesScript>().PurchaseForGold(myGoldCost)) || !isItemSold) { //destroy the price text if (isItemSold) { transform.parent.gameObject.GetComponent <SpawnItemScript>().DestroyPriceText(); } isItemSold = false; //display the consumable item on the UI Image image = GameObject.Find("ConsumableItemUI").GetComponent <Image>(); image.sprite = itemSprite; Color tempColor = image.color; tempColor.a = 1f; image.color = tempColor; timeSincePickup = 0; ItemsManagerScript itemsManager = GameObject.Find("ItemManager").GetComponent <ItemsManagerScript>(); //if the player has no consumable item held if (itemsManager.PlayerConsumableItem == null) { //get the new item and destroy the prefab of the object itemsManager.PlayerConsumableItem = itemName; SetName(itemsManager.PlayerConsumableItem); Destroy(gameObject); } else { string tempName; //swap player's consumable item with the one on the ground tempName = itemName; SetName(itemsManager.PlayerConsumableItem); itemsManager.PlayerConsumableItem = tempName; } } } }
// Start is called before the first frame update void Start() { GameObject itemManagerGameObject = GameObject.Find("ItemManager"); ItemsManagerScript itemManagerScript = itemManagerGameObject.GetComponent <ItemsManagerScript>(); List <string> myItemList; //get the correct items list if (isEquipment) { myItemList = itemManagerScript.GetItemsEquipmentList(); //if a name is specified, spawn the requested item if (specificItemName != "") { itemSpawned = itemManagerScript.CreateEquipmentItem(transform.position, specificItemName); } //if no name is specified, spawn a random item else { itemSpawned = itemManagerScript.CreateEquipmentItem(transform.position, itemManagerScript.SelectRandomItem(myItemList)); } } else { myItemList = itemManagerScript.GetItemsConsumableList(); if (specificItemName != "") { itemSpawned = itemManagerScript.CreateConsumableItem(transform.position, specificItemName); } //if no name is specified, spawn a random item else { itemSpawned = itemManagerScript.CreateConsumableItem(transform.position, itemManagerScript.SelectRandomItem(myItemList)); } } itemSpawned.transform.SetParent(this.transform); //if we want the item to be sold in a shop if (isItemSold) { //set the text showing the price priceText = Instantiate(itemPriceGameObject, transform).GetComponent <Text>(); priceText.transform.SetParent(gameObject.transform.GetChild(0).transform); priceText.transform.position = new Vector2(transform.position.x, transform.position.y - 0.6f); //change the item's price if (isEquipment) { itemPrice = 100; } else { if (itemSpawned.GetComponent <Item>().GetName() == "Silex") { itemPrice = 35; } else { itemPrice = 50; } } itemSpawned.GetComponent <Item>().SetIsItemSold(true, itemPrice); priceText.text = itemPrice.ToString(); } }