#AudioSource not playing from system

1 messages · Page 1 of 1 (latest)

granite dome
#

This code doesn't work and I dont' know why, I just want to play an AudioSource when the Entity Gameobject from Prefab is spawned, AudioSource is on the GameObject's child with his Authoring component

queen lagoon
granite dome
queen lagoon
granite dome
#

It should still make a companion GameObject with an AudioSource, right?

queen lagoon
#

And create AudioSources in runtime

#

In new GameObjects

queen lagoon
#

Something like this is implemented only in Netcode for entities

granite dome
#

Because something is happening here

#

Engine Audio source is just a GameObject with Audiosource

queen lagoon
queen lagoon
granite dome
#

But why doesn't work then

queen lagoon
queen lagoon
#

Ok

granite dome
#

CarAudioModule is a scriptableobject with settings

queen lagoon
#

I have not heard of an official implementation of audio systems for entities.

granite dome
queen lagoon
#

For this you need system

granite dome
#

Yea, i know

queen lagoon
#

Which is not implemented

#

AudioSource implemented only for GameObject update loop

granite dome
queen lagoon
#

Idk why it works

#

Maybe it can only work in the editor but not in the build

granite dome
#

maybe because awake is called differently from update, and still works on entity world

#

in someway

#

Example:

#

This works even outside play mode

#

But it only works when AudioSource play on awake is checked

queen lagoon
#

So it shouldn't work in the build

granite dome
#

@queen lagoon I can see that there are a bunch of systems for graphics and physics already working but nothing for the audio so it's probably that

queen lagoon
#

You can use GameObjects pool with AudioSource components

#

Or use other audio systems like FMOD

thorny fulcrum
#

you should not need your own system or anything like that

#
    internal class CompanionComponentSupportedTypes
    {
        public static ComponentType[] Types =
        {
            typeof(Light),
            typeof(LightProbeProxyVolume),
            typeof(ReflectionProbe),
            typeof(TextMesh),
            typeof(MeshRenderer),
            typeof(SpriteRenderer),
            typeof(VisualEffect),
            typeof(AudioSource),
            typeof(SphereCollider),
            typeof(BoxCollider),
            typeof(CapsuleCollider),
            typeof(MeshCollider),
#if PARTICLE_SYSTEM_MODULE
            typeof(ParticleSystem),
            typeof(ParticleSystemRenderer),
#endif
#if SRP_7_0_0_OR_NEWER
            typeof(Volume),

#endif
#if URP_7_0_0_OR_NEWER
            typeof(UnityEngine.Rendering.Universal.DecalProjector),
#endif
#if HDRP_7_0_0_OR_NEWER
            typeof(UnityEngine.Rendering.HighDefinition.DecalProjector),
            typeof(HDAdditionalLightData),
            typeof(HDAdditionalReflectionData),
            typeof(PlanarReflectionProbe),
            typeof(LocalVolumetricFog),
#endif
#if URP_7_0_0_OR_NEWER
            typeof(UniversalAdditionalLightData),
#endif
#if HYBRID_ENTITIES_CAMERA_CONVERSION
            typeof(Camera),
#if HDRP_7_0_0_OR_NEWER
            typeof(HDAdditionalCameraData),
#endif
#if URP_7_0_0_OR_NEWER
            typeof(UniversalAdditionalCameraData),
#endif
#endif
#if SRP_17_0_0_OR_NEWER
            typeof(ProbeVolume),
            typeof(ProbeVolumePerSceneData),
#endif
        };
    }```
unity supports companion gameobjects for these components
queen lagoon
thorny fulcrum
#

no idea sorry, never read it

#

documentation was non-existent when i learnt dots so i've always learnt from just reading the source

queen lagoon
thorny fulcrum
#

it's been a feature since 1.0

queen lagoon
#

Interesting

thorny fulcrum
#

the graphics documentation mentions it

#

as it can optionally enable companion objects for the camera

granite dome
queen lagoon
queen lagoon
thorny fulcrum
#

but yeah if you just put an audiosource in your scene

#

it will auto attach/create it as a companion object

#

setup all the transform syncing etc

granite dome
#

@thorny fulcrum Thank you I'm not an idiot then

queen lagoon
thorny fulcrum
#

i haven't used it in ages as i have my own companion solution

#

that automatically adds a bunch of other stuff like pooling

#

so i actually force disable audio sources from companion objects

#

but just loading in test project, yeah working fine

queen lagoon
#

So we has companions in just ecs and ghost presentations in Netcode

thorny fulcrum
granite dome
#

@thorny fulcrum I think the problem is just that I call Play in the system, if I disabled and enable the component should work just fine

#

with play on awake enabled

thorny fulcrum
#

that is odd, i do remember having weird issues with audio sources in the past

queen lagoon
#

It may break a few more times later

thorny fulcrum
#

it's very supported/official

#

the question is really more just, what happens in 6.6

#

i asked unity about this a while ago and they were like, they probably don't need companion objects in 6.6 but they will probably continue to exist to start with

granite dome
thorny fulcrum
#

that said, it'll probably just work exactly how you're using it

#

just with the gameobject/components directly on the entity

#

just not via the component system anymore

#

but until 6.6 drops we can't say for certain how integration will work

granite dome
#

I will do only tests with Audiosource and I will comeback with the results

#

without doing pools, but simply using the companion

#

just to understand

queen lagoon
thorny fulcrum
#

why would it be?

#

it's an internal solution that you don't interact with

#

and if replaced in 6.6 the user would interact with it the same way

#

directly interacting with the audio source

queen lagoon
#

Also netcode for entities has self companion system

thorny fulcrum
#

that is definitely being removed at some point

#

in favour of their new gameobject workflow they just added backend in 1.11

#

which will be eventually merged to 6.6 entities changes

#

but the last change to companion system was in 1.3.14

#

so not sure what you mean by not updated since 2022

#

it was updated in 2025

thorny fulcrum
queen lagoon
thorny fulcrum
#

(vs the old netcode gameobject presentation layer)

thorny fulcrum
#

it's 1 line of code

#

you add type to list, that's it

granite dome
#

Guys, it works, just fine

queen lagoon
thorny fulcrum
#

you can even make anything work with it btw with internal access

granite dome
#

I did a simple Play every tot seconds

thorny fulcrum
#
            var types = CompanionComponentSupportedTypes
                .Types
                .Concat(new ComponentType[]
                {
                    typeof(CinemachineCamera),
                    typeof(CinemachineFollow),
                    typeof(CinemachinePositionComposer),
                    typeof(CinemachineSplineDolly),
                    typeof(CinemachineOrbitalFollow),
                    typeof(CinemachineRotationComposer),
                    typeof(CinemachineFreeLookModifier),
                    typeof(CinemachinePanTilt),
                    typeof(CinemachineRotateWithFollowTarget),
                    typeof(CinemachineHardLockToTarget),
                    typeof(CinemachineHardLookAt),
                    typeof(CinemachineSplineDollyLookAtTargets),
                    typeof(CinemachineBasicMultiChannelPerlin),
                    typeof(CinemachineGroupFraming),
                    typeof(CinemachineFollowZoom),
                    typeof(CinemachineCameraOffset),
                    typeof(CinemachineRecomposer),
                    typeof(CinemachineVolumeSettings),
                })
                .ToList();```
#

now, cinemachine works with it!

queen lagoon
thorny fulcrum
#

^ what he said

#

SystemAPI.ManagedAPI.UnityEngineComponent<AudioSource>()

queen lagoon
thorny fulcrum
#

i've thought about this a bit but

#

why would you want to

#

there's overhead everytime you call UnityObjectRef.Value

granite dome
#

Like this:

thorny fulcrum
#

yep, just saves you doing the exists check

granite dome
thorny fulcrum
#

but i suspect most users don't even know this is an option

queen lagoon
granite dome
#

Guys where should I use UnityObjectRef?

queen lagoon
thorny fulcrum
#

i doubt you can be faster than this

queen lagoon
thorny fulcrum
#

vs doing an engine call

granite dome
queen lagoon
#

UnityObjectRef is ref to managed component but UnityObjectRef is unmanaged component

granite dome
queen lagoon
#

*is not component is field for unmanaged component

queen lagoon
granite dome
#

right?

queen lagoon
#

Idk

granite dome
#

It's just the examples that I showed you before

#

Nothing more

queen lagoon
#

I think it's better to use UnityObjectRef only in components that should be structures

thorny fulcrum
#

100,000 calls

queen lagoon
thorny fulcrum
#

i mean you're not having 100k gameobjects

#

so i doubt for most cases that unityobjectref is ever going to be a problem

queen lagoon
thorny fulcrum
#

for the record, i use unityobjectref myself

#

for my companion solution

#

because i'm aiming to disable support for managed components

granite dome
#

Yep just did it with unityobjectref, works just fine

#

passing it with baker

#

Thank you guys really boosted my knowledge

thorny fulcrum
#

slightly concerned about this for you in builds

#

i'd check this still works

#

i'm pretty sure the companion object isn't the same as the object in the unityobjectref

#

so i think you're duplicating sources

#

but it's been ages since i looked at this

granite dome
#

could be, make sense

#

@thorny fulcrum Yep baker clones it

#

name is ...(clone)

queen lagoon
queen lagoon
granite dome
queen lagoon
#

ok

granite dome
#

companion it is

#

UnityObjectRef could be usefull for clips for examples I think

#

if passed trough baker

#

or scriptable objects?

queen lagoon
granite dome
#

Everything starts to make sense to me, thank guys

#

@queen lagoon @thorny fulcrum May I close the post?

queen lagoon
#

Yes

granite dome
queen lagoon
granite dome
#

It should be like in the main Entities DOCS

#

I hope they will unify this

queen lagoon
granite dome
#

@queen lagoon I was always going back and forward trying to understand what was happening and then I realized this😁

queen lagoon
granite dome
#

@queen lagoon Official Unity Staff confirmed missing and misleading DOCS