Хорошо, у меня очень странные проблемы. Я сейчас пишу простую игру в C#/MonoGame (в Linux). Я пытаюсь сыграть SoundEffect
. Когда я звоню Play()
(хотя он был правильно загружен в методе LoadContent()
). Он бросает NullReferenceException
с сообщением Object Reference not set to an instance of an object
.SoundEffect.Play() выбрасывает исключение NullReferenceException даже при загрузке звука
Вот как код структурирован
public class MyGame : Game
{
// ..
private SoundEffect _sfx;
public PingGame()
{
// ...
}
protected override void Initialize()
{
// ...
}
protected override void LoadContent()
{
// ...
// No errors here on loading it
_sfx = Content.Load<SoundEffect>("noise.wav");
}
protected override void Update (GameTime gameTime)
{
// ...
if (playSound)
{
// This is where the error is thrown
_sfx.Play();
}
// ...
}
protected override void Draw (GameTime gameTime)
{
// ..
}
}
Может быть, вы столкнулись с этим вопросом [] (http://community.monogame.net/t/null-reference-exception-when-calling-play-on-a-soundeffect-or -soundeffectinstance/7319/10) – craftworkgames