I am making a shop in a game. I have 3 pedestals in the shop, and each time you start the game, the shop will randomly generate three different items from an item pool that will be placed on each of the pedestals. These items will be child GameObjects on each pedestal.
I am trying to reference the child's script, so that I can call a method from it that would apply the item's effects to the player. However, I am unsure on how to reference this script component. I can successfully reference the child GameObject using transform.GetChild(0).gameObject but I am unsure on how to access the script component of the child since each child will have its own unique script with its own unique effects.
Here is my parent GameObject's script:
public GameObject item;
void Start()
{
item = transform.GetChild(0).gameObject;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
BuyItem();
}
}
void BuyItem()
{
Destroy(item);
// call "ApplyItem()" from child gameobject's script
}
Child GameObject script:
public void ApplyItem()
{
player.fireRate -= 0.1f;
player.bulletSpeed += 50;
}