Monogame SoundEffect Crash - c#

I am trying to re-program some old Arcade games in C# with the Monogame engine. However, I have encountered a certain problem.
Since my code tends to be a bit messed up, I am often not taking the effort to reset everything when the player successfully completes the game. Instead, I am simply closing the current Game-instance and opening a new one, like this: (in Program.cs)
if (startgame)
{
do
{
using (var game = new Game1(level, points, soundOn))
game.Run();
} while (continueGame == true);
}
Now the problem. In Game1, I am declaring and playing various SoundEffects. The first run everything works fine, but in all following Game1-instances, my program will always throw an System.AccessViolationException related to SharpDX.XAudio2.dll at the moment I am calling the .Play()-Method of a SoundEffect.
I tried playing SoundEffectInstances instead of the actual SoundEffects. Now it doesn't crash anymore, but is being completely silent from level 2 on instead.
Do you know what could be the reason of this error? Is my game-restarting loop causing problems I did not know about?
Thank you in advance.
(I am using Win 7 64 Bit, VS Express 2015 and Monogame 3.6)

Do you know what could be the reason of this error? Is my game-restarting loop causing problems I did not know about?
Yes. Do not do it.
You are destroying the whole game object just to reset the game. This is a very bad style, because it destroys the whole instance and everything that is connected to it. If you use static variables within it, they will remain in memory which may be the problem you have.
If you want to reset your game, simply write a Reset-method for everything and call these instead. Cleanup and nice code is something you should use instead of terrible hacks.
Can you show more code, so it is possible to understand, what went wrong?
PS: Never write something like while (continueGame == true) always go for while (continueGame).

There is a chance that the issue is due to SharpDX's reaction to the media file itself. For instance, an MP3 file that would work perfectly fine under XNA 4.0 loaded as a SoundEffect (with associated SoundEffectInstance) may crash the game under MonoGame UWP. (This happened to me with one file, but I was already using SoundEffectInstance in the first place when the crashing started.)
As such, have you tried re-encoding your relevant audio files, such as through a utility like Audacity? Although it may not solve the internal issue per se (some element of the audio file breaking SharpDX), if a re-encoded file doesn't have the problem element in the first place, that should be a valid solution for you.

Related

All my Unity projects contain blank compilation errors (only the timestamps), preventing any projects from working or importing correctly

I really need some help. I was rearranging file locations and trying to clear up some space and somehow broke the ability for Unity to operate on my laptop. The pictures of the errors are below.
This all started when I was moving a completely functional unity project from an external SSD to my internal laptop SSD. I had to clear space for the size of the project and I must have deleted something important, but I have no clue how to tell what it is.
Error seen when opening project
Editor view after opening
Error when trying to attach a script to a GameObject
Error in editor log
Steps I've taken:
I've restarted my computer several times.
I've completely uninstalled all editor versions and the unity hub (twice), including deleting all local app data for unity, emptying the temp bin, and clearing the keys in the registry.
I've started completely new projects, checked
old ones that used to work, downloaded sample ones from unity learn
just to make doubly sure that it's not me being dumb.
My current theory is that this has to do with me clearing some data from a National Instruments folder. I'm new to programming and just read online that these drivers are needed for .NET communication, but would this be why Unity keeps failing then?
This has all taken more than a few hours and I'm struggling to fix this if anyone has any suggestions, please. I'd really appreciate any insight.
Update: I'm able to make new scripts, objects, scenes, etc. but I can't attach any script components to game objects because "the script class can't be found"
Update #2: Checking the Editor logs shows that the Tundra build is failing, and the "93 items updated" matches the 93 errors that are shown. No idea what Tundra is, but that's something to look at now!
Update #3: fairly more certain it's something to do with the .NET interactions, but I'm completely unknowledgeable about this and worried about breaking this further
Answering my own question in case another person finds this and has the same issue, but the problem wasn't .net related or anything I mentioned. I uninstalled anaconda which caused some issues with the Command Prompt, where it would close immediately after opening. Unity uses this for a number of processes, so that's what caused all the blank errors.
The solution is to delete the Microsoft Command Processor AutoRun key in the registry editor.
https://forum.unity.com/threads/unity-empty-errors-on-every-project-unity-2021-3-2f1.1279124/
Cmd crashes with exit code 1 after uninstalling anaconda

Unity Prefab Not Updating On Old Android Tablet

I have a Samsung Galaxy Tab Active 1, on the latest version of android it can handle, and just recently, after adding accessibility features that shouldn’t affect anything, the balls in my game stopped updating. I instantiate a ball, and change a few properties on it, and then for some reason I think it’s not calling the update method. There are no errors.
I’ve heard using PrefabUtility to instantiate might help, but I haven’t done that just yet. I will update if PrefabUtility works.
Any advice?
Whoops, this was due to me being a bit stupid. The main reason it was happening was because of a parsing error in some of the accesibility settings, as they were not saved correctly due to weirdness I can't wrap my head around with files in Android. TLDR: file did not save correctly, and so did not load correctly, and so data messed up, and so it errored before it could do anything.

Make a unity game look like it has crashed C#

I want to make my Unity game look like it has crashed (Unresponsive Application Crash, Not a code exception crash).
There probably is some function in the Windows API you can call to emulate this.
I have also seen a game called "Pony Island" do this before, so it certainly is possible. However I don't own a copy nor can I decompile a steam game.
If there is a way to actually hang the program while still being able to bring it back at anytime that solution is preferable, this seems to be unlikely.
To be clear, I want it to look legitimate, so I would prefer the windows API be used to change the window color and generate a pop-up, instead of having them in unity as GUI objects or something similar. I also don't really care about cross-compatibility because the Mac API appears to be hard to implement and even harder to test (on a Windows machine), and most likely won't be compatible with as many hacky features as the Windows API is.
Best way to do this safely would be to run some script setting time.timeScale = 0 - effectively 'freezing' the game, while simultaneously playing the windows 'error' sound effect.
(Note: You may have to also pause audio sources and particle emitters from this script, I've not used timescale before an am unsure if it affects these)
I know you suggested against using UGUI but an image with no source placed over the entire screen, with it's colour and alpha tweaked could very easily emulate the 'frozen screen' too.
You could also look into ways to forcibly 'restore'/change fullscreen mode of the window (if it is played in full screen)
I would try to achieve that by using Thread.Sleep(int)
so while in the main thread, i would call that to freeze the entire unity loop for an amount of seconds eg 5 seconds = 5000ms
and check whether you want to continue the crash or not. for example, but not tested, (i don't have unity at my office environment)
Be carefull with this, as this will freeze it for real, you can't do any operation while it still freezing.
public class Crasher : MonoBehaviour
{
public bool isCrash = false;
//The crash in seconds
public float crashDuration = 5;
void Update ()
{
if (isCrash)
{
//Freeze the applicaion for amount of milliSeconds
System.Threading.Thread.Sleep(crashDuration * 1000);
}
}
}

SceneManager.LoadScene is crashing for Android Devices

Ok, I've been testing a game with several scenes. In Unity and iOS devices, everything works perfect. In Android, always crash (or freeze if you prefer).
I'm using Unity 2017.2.0f3. and the line of code that crash is the next one (nextScene is a string with the value of the next scene):
SceneManager.LoadScene (nextScene);
I've been trying other options like:
SceneManager.LoadScene (nextScene, LoadSceneMode.Single);
But this is not working neither. The last one I tried was:
SceneManager.UnloadSceneAsync(UIManager.Instance.currentScene);
SceneManager.LoadScene (nextScene, LoadSceneMode.Single);
Again without success. Please I really need help with this. I've dealing with this problem for 24 hours. It's driving me crazy.
After more than 24 hours I solve the problem. I hope that my solution help other people because there isn't enough references on the web.
When I created the APK file it was splitter with a second one of type OBB. This kind of files is required when the Android App is too big. In my case was set by default.
The solution was to force the creation of the APK without the OBB. To do that you must get to Player Settings in Android, go to Publishing Settings and uncheck Split Application Binary (located at the bottom).
Everything's working now and life is good again.

Why is Unity crashing when I press play in the editor?

My game is crashing whenever I switch scenes and then press play in the editor. I am using this code to switch scenes:
...
I have tried disabling most of the scripts in my game to debug the problem but it freezes every time. I've also tried loading the levels without the co-routines but that doesn't help. I can continue playing the game as long as I don't use the editor controls, however things get pretty choppy and performance becomes terrible.
First, look at the editor logs, quite possible that the reason of the crash will be there. If unsure, post them here (post on gist and link here).
Also, if you have unity pro (or a trial version of pro), try looking at the profiler's readings. On free you can also try looking at the Stats button from the Game window. Using too much memory (RAM or VRAM) can cause crashes.
i was also having same problem. if your program is correct then just change the editor path to anything in unity hub and save, close unity hub, open again and set the path to previous one.

Categories