AttachInstanceToGameObject not moving the sound position

I’m working with unity, fmod 1.09.06 and the oculus spatializer and I have issues using AttachInstanceToGameObject.
I have isolate the problem:

  • i create the istance of the object.
  • i call the AttachInstanceToGameObject.
  • if i move the obj the sound stays where it was created.

I do this simple thing with the following lines:

eventInst = FMODUnity.RuntimeManager.CreateInstance (asset); FMODUnity.RuntimeManager.AttachInstanceToGameObject (eventInst, this.transform,this.GetComponent<Rigidbody> ());

This doesn’t move the sound while i move the object, and I also get this warning:

FMOD Studio: Instance of Event event:/04_OBJECTS/OBJ_Diorama/OBJ_Diorama_Gear_Blocked has not had EventInstance.set3DAttributes() called on it yet! UnityEngine.Debug:LogWarningFormat(String, Object[]) FMODUnity.RuntimeManager:Update() (at Assets/Plugins/FMOD/RuntimeManager.cs:412)

What am I doing wrong?

I’ve also tried using the function
eventInst.set3DAttributes (FMODUnity.RuntimeUtils.To3DAttributes (this.transform));
in the update and everything works fine, but it’s not a clean solution in my project.

Thank you for your help,
Simone

1 Like

It looks like it is set up correctly, when are you calling create/attach?
I am able to get it working using the following:

public class test : Monobehaviour
{
[FMODUnity.EventRef]
public string eventName;

FMOD.Studio.EventInstance eventInst;

void Start ()
{
    eventInst = FMODUnity.RuntimeManager.CreateInstance(eventName);
    FMODUnity.RuntimeManager.AttachInstanceToGameObject(eventInst, this.transform, this.GetComponent<Rigidbody>());
    eventInst.start();
}
}

I’ve tested your code and it’s working, so i’ve spent a few hours testing a bit more and i discovered that the problem occurs when i don’t do eventInst.start() during the same frame of the AttachInstanceToGameobject.

I recreated the error starting the event in a coroutine after a few seconds:

void Start ()
{
eventInst = FMODUnity.RuntimeManager.CreateInstance(eventName);
FMODUnity.RuntimeManager.AttachInstanceToGameObject(eventInst, this.transform, this.GetComponent());
StartCoroutine(startCoroutine(3));
}

private IEnumerator startCoroutine(float delay)
{
yield return new WaitForSeconds (delay);
eventInst.start ();

}

Can you try this and tell me if you have the same problem?

Normally you would need to call Set3DAttributes on an event to update its position in the world, but in the Unity wrapper we supply AttachInstanceToGameObject as a helper/replacement.

When you use AttachInstanceToGameObject, the instance is added to a list of events that have Set3DAttributes called in the RuntimeManagers Update function. To ensure that instances aren’t being updated that do not need to be, there are a few conditions that can cause an instance to be removed from the list and not have their position updated: instance.isValid(), transform == null, and playbackState == stopped.

If the event isn’t playing by the time it does the check, it will be removed from the list and not updated.

1 Like

Ok, good to know.
Is there any complete documentation for the API? This problem cost me hours of work to retrieve an information that i could have had simply by reading it in the description of the metod.
The only documentation I found is this: http://www.fmod.org/documentation/#content/generated/common/introduction_web.html
and i can’t find a lot of function in there, like AttachInstanceToGameObject or isValid (in fact I still don’t know what “valid” means in this contest…).
I’ve been working with FMOD for a year now, but there are a lot of basic notions that I couldn’t find anywhere. Am I missing something? Is there a more complete documentation that i’m not aware?

Anyway, thanks for your help
Simone

3 Likes

“Hello! I’m having the same issue. I have an instance that starts when the object moves. However, the sound stays at the initial position of the movement.
I called the AttachInstanceToGameObject function only in Awake. Should I also call it in Update? Additionally, for extra context, my event has a Spatializer.”

I do not have the code at hand, but if I remember correctly, you need to call the AttachInstanceToGameobject when you start your sound.

If you do that in the awake function and your sound is not starting in the awake, it is removed from the list of the “sounds to keep updated” and it’s position remain still. This is done for performance reasons and it make sense, to avoid having garbage objects that have no sounds, but are still in a list to be updated.

If you want to be shure, try to look for more info on how FMOD manages the list of gameobject that has a sound attached to keep updated, maybe looking at the source code.

Usually you want to call AttachInstanceToGameobject as you are ready to play the Event, as attaching it before it’s played has no affect on the Event itself.

If you call AttachInstanceToGameobject it adds the Event to a list for the RuntimeManager to keep track, the next RuntimeManager::Update after that checks for any stopped Events in the list so that they can be removed. If you do not call start on the Event then it will come back as stopped and be removed from the list.