How to play sound when using ConsumableItem?

yuukimoka

New member
Specify the voice "Play Sound" in Item Definition in Inventory manager
How do I access this audio with an item action?
 

Sangemdoko

Moderator
Staff member
Yes that's correct. You can create an Attribute with the AudioClip type.
1600329726496.png

If you plan to use the same sound for all consumables you can set the audio on the category, if you plan to make it different for some consumables then you can set it in the item definition attributes.

In the itemAction you can access it by getting the attribute value, something like this:

Code:
// Retrieves an audio clip attribute value.
if (itemInfo.Item.TryGetAttributeValue("Sound", out AudioClip audioClip)) {
    // audioClip exists.
    AudioSource.PlayClipAtPoint(audioClip, itemUser.transform.position);
}

NOTE: the code above is untested so there may be some errors.

More detail on getting and setting attributes at runtime here:
 
Top