The question is answered, right answer was accepted
I'm getting an error: "AudioClip can't be deserialized because it has no default constructor"
I'm trying to add voice chat functionality to my multiplayer game. I'm using Mirror for the multiplayer. Unity is returning "AudioClip can't be deserialized because it has no default constructor", and "Weaving failed for: Library/ScriptAssemblies/Assembly-CSharp.dll"
Any suggestions would help, I don't understand what's causing the issue.
Here's my code:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Mirror;
public class VoiceChatManagerScript : NetworkBehaviour
{
public string micName;
[SyncVar(hook = nameof(PlayVoices))]
List<Voice> currentVoices = new List<Voice>();
[SyncVar]
float timer = 0;
Voice voice;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void PlayVoices(List<Voice> oldVoices, List<Voice> voices)
{
if (Microphone.devices.Contains(micName))
{
Microphone.End(micName);
CmdSendCurrentVoice(voice);
foreach (Voice v in voices)
{
AudioSource.PlayClipAtPoint(v.clip, v.position, 1f);
}
voice = new Voice();
voice.position = GameObject.FindGameObjectWithTag("localPlayer").transform.position;
voice.clip = Microphone.Start(micName, false, 1, 44100);
}
else
{
foreach (Voice voice in voices)
{
AudioSource.PlayClipAtPoint(voice.clip, voice.position, 1f);
}
Debug.Log("Unknown mic name specified, please enter the correct name.");
Debug.Log(Microphone.devices);
}
}
[Command(ignoreAuthority = true)]
void CmdSendCurrentVoice(Voice currentVoice)
{
currentVoices.Add(currentVoice);
}
}
public class Voice
{
public AudioClip clip;
public Vector3 position = Vector3.zero;
public Voice() { }
}
Answer by nathonion · Jan 18, 2021 at 03:43 PM
I was able to figure out the problem, so for any in the future with a similar problem, I'm pretty sure what it means by deserializing is Mirror trying to turn AudioClip into a string of bits, to be sent over the internet. According to the error, apparently you can't do that if the variable doesn't have a constructor, which AudioClip doesn't. A workaround for this would be to send over a float array that has the sample data, and then in a command convert that back into an AudioClip, which I have done in my new code, that works. (Also of note, the code I posted in the question has a lot of other problems with it, like doing syncvar to a list. Basically, don't use it as example code!)
@Kadir- sorry, but this question was posted over a year ago. While I probably should have posted the updated class in the answer back then, I don't even have that code anymore so I definitely can't do it now (not to mention my code was probably garbage and wouldn't work anyway). But if you're having the same issue, it looks like if you just read the docs for AudioClip, you'll probably see how to get a float array and then you can replace the AudioClip variable with that. Again, sorry for not being so thorough back then, but this is the best I can do now.