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!
Could someone help me please? Thank you in advance!
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).
I changed “void play_sound()” into “public void play_sound()”, but still doesn’t work.
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()”.
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.