Sound linked to animation of a object doesn't work

Hello! I am building a VRChat world oriented to sound experience, and I want to link some sounds to the animations of some objects (animation regarding movement or lighting of objects).

I am trying to link a sound to the rotation of a cube object, so I have added a animation event in the animation clip. In this animation event I selected a function called “play_sound ()” that I implemented with a script attached to the cube object. Here’s the script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Audio_play : MonoBehaviour
{
public AudioSource aud;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

void play_sound()
{
    aud.Play();
}

}

I have selected the sound source, disabled “play on awake”, and tried it out several times, but it doesn’t work. The fact is that i tried this process with a non VRC world, and there it works! :face_exhaling:

Could someone help me please? Thank you in advance!

1 Like

You may need to change the signature of your method to
public void play_sound() instead of just void play_sound()

If you don’t specify public, then your method is going to be private by default. Private methods cannot be called from elsewhere, including animation events.

In addition, you may need to change the event name in your animation event.
If it’s play_sound(), you need to change it to play_sound (without brackets).

1 Like

Hi Fax, thank you for responding!

I changed “void play_sound()” into “public void play_sound()”, but still doesn’t work. :face_with_diagonal_mouth:

The event name in the animation event is already “play_sound” without brackets (as I see when i have my mouse cursor on it), even though in the selection window in the inspector it makes me choose it as “play_sound()”.

Any other guesses?

1 Like

Make sure you’re using the SendCustomEvent ( String ) function in the animation event. Choosing the public function from the dropdown will not work!

Make sure that the animator is on the same GameObject as the UdonBehaviour you’re trying to send events to.

It’s possible that there’s an issue with your Audio Source. You could add a Debug.Log("Something") in addition to playing the sound.

1 Like

I finally managed it!

First of all I had to add a Udon Behaviour (script) component to the GameObject, and then drop my script into its “Program Script” item.

Then, I changed the script in this way (I found out that “MonoBehaviour” doesn’t work in VRC! ) :

using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;

public class Udon_2_Audio_Play : UdonSharpBehaviour
{
public AudioSource aud;
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

public void udon_2_play_sound()
{
    aud.Play();
}

}

After this, in the dropdown menu of the animation event in the animation clip, I chose “SendCustomEvent ( String )” like Fax suggested, and then in “Parameters String” item I entered the name of my function (“udon_2_play_sound”).

I hope this can help anyone stuck with this problem.

Thank you again Fax :slightly_smiling_face:

1 Like

You’re welcome! Glad you found a solution.

If you come from C# development you may want to check the UdonSharp documentation to see what is and isn’t supported.

1 Like