Hi, Im making a world with a lot of boxes that play audio when collected. I don’t want to have various audio sources on every single one so I thought I’d add them in during runtime. Unfortunatley, both Udon and UdonSharp seem to be missing the gameobject.AddComponent line. I tried referencing my own script but I guess custom scripts aren’t allowed. Why cant I add components during runtime and/or is there a way to get around this without having 20+ of these audio sources existing at once?
AddComponent is not exposed to udon for security reasons. You’ll have to do it without that.
If you want to add audio sources as you need them, you could use an object pool to grab one, play on it, then put it back when done. The VRC Object Pool is inherently synced and would likely not be what you’re looking for, but in general having an array of objects to pull from would be a pretty effective way to grab them as needed, and you won’t run into the performance overhead of creating objects or audio sources as they’ll already be available in the scene.