2

I am trying to Set audioclip samples previously saved using audioclip.GetData from a json file. The Audio data is present in my json file but it seems not to work when Loading this file and setting up the audioclip data using SetData:

In my Load function: I am Instantiating a prefab that contains the component AudioSource: audioFB. Then I want to set up the clip using:

audioFB.GetComponent<AudioSource>().clip.SetData(qc.clipData, 0);

However I do have this error: Object reference not set to an instance of an object when doing so.

On the other hand if I directly Load an audio file from Resources folder, it does work.

Am I not using SetData correctly?

Thank you for your help.

FSebH
  • 33
  • 5
  • Did you check that audioFB is not `null`, also that the `GetComponent` isn't returning null as well? – AresCaelum Nov 16 '18 at 15:06
  • Thanks for the quick answer! Yes I checked audioFB, I can set other attributes and it works fine. As I said, even if I add an audioclip from my resources folder it works, but using SetData it returns the object reference error. Same goes for GetComponent, not returning null. – FSebH Nov 16 '18 at 15:18
  • Did you also check that your AudioSource.clip isn't null? – AresCaelum Nov 16 '18 at 15:26
  • When I instantiate the prefab with its Audio Source component, It has a predefined sound. I am trying to use SetData to modify this predefined sound's data, am I doing it the wrong way? PS: If I do it this way, I don't get the error but my sound is not modified/changed with the new data I want to SET. However without pre-adding the sound I do get the object reference error. – FSebH Nov 16 '18 at 15:30
  • Possibly, from what I see there are a few places this can be going wrong they are: audioFB is null, the AudioSource is null, the AudioSource.clip is null(It could still be being created by the time this method is called?), or qc is null. I would just write a quick check on each of those and have it print out which is null. – AresCaelum Nov 16 '18 at 15:33
  • I finally managed to make it work when I am adding a "silence" sound (0 amplitude) in my instantiated object clip. Then I use SetData(qc.clipData,0). Now my issue is that for each object I make, I always get the same sound even if qc.clipData is different for each of them.. – FSebH Nov 16 '18 at 15:55

0 Answers0