Question by
JetixBoy · Apr 08, 2021 at 12:54 AM ·
listgetcomponentcollider2dontriggerenter
how to get a string from another script in OnTriggerEnter2D
Im working on a inventory to my game and I can put the image of the object with the follow script.
void OnTriggerEnter2D(Collider2D coll){
if (coll.CompareTag("Food")) {
takeItems(FoodBag, coll);
} else if (coll.CompareTag("Weapon")) {
takeItems(WeaponBag, coll);
} else if (coll.CompareTag("Outfit")) {
takeItems(outfitsBag, coll);
}
}
public void takeItems(List<GameObject> list, Collider2D collider){
for (int i = 0; i < list.Count; i++) {
if (list[i].GetComponent<Image>().enabled == false) {
list[i].GetComponent<Image>().enabled = true;
list[i].GetComponent<Image>().sprite = collider.GetComponent<SpriteRenderer>().sprite;
break;
}
}
}
As you can see, im going to use three diferent inventorys from my game. But now I want to display two values from the item that my character is going to take, one of them is
1.- The name of the item
I also have a script from my items and i've the follow code:
public string ItemName;
public int health;
void OnTriggerEnter2D(Collider2D otherObject){
if (otherObject.tag == "Player") {
Destroy(gameObject);
}
}
At first I thought that i only would have to put this in my method just like i did with the images:
list[i].GetComponent<ItemBehaviour>().ItemName = collider.GetComponent<ItemBehaviour>().ItemName;
but didn't work, basically i want to save my string from ItemBehaviour script in my inventory script. i'd appreciate any comments or suggestions. regarts!
Comment