Soundeffects not working. Could not load ... asset - c#

So I'm trying to implement soundeffects whenever I click on an item. I've added a .wav sound to my Content folder.
Then I typed the following code in the Game1 class constructor:
SoundEffect soundEffect;
After that I wrote the following code in the Initialize function:
soundEffect = Content.Load<SoundEffect>("Laser_Shoot25.xnb");
Finally, I wrote the following code in my clickFunction:
soundEffect.Play();
When I tried running it, it gave me the following error.
Could not load nameOfSoundFile asset.
I've looked on Google to see if someone else had this same problem, but none of those solutions seem to work for me.
Does anyone know what else I'm supposed to do to make this work?

First thing that comes to mind is the possibility of you loading your assets before the Content object gets initialized. Please post the relevant code where you load your asset to know if this is the case (usually the initialize and LoadContent methods of your Game1 class).
The other possibility is the asset file format.
I'm not up to date with Monogame as I stopped using it with version 3.2, but if things remain the same on this aspect; then for working with sounds and spritefonts you need to add the asset as an exported xnb file, created by using the actual XNA content pipeline.
I have a blog post that explains exactly how to do all of this, which can be found here.
First, you need to install XNA on your machine (in the same post, it explains how to do it if you have Windows 8 and/or Visual Studio 2012/2013).
Then, just follow the steps detailed on the post:
So, once we have this, let’s create a new Windows Game project (it has
to be a game and not only a Content project as we will need to access
the game output directory).
After having this done, we can add our sound effects/instances to the
Content project by right clicking, selecting “Add existing item” and
choosing it in the folder explorer.
Before continuing, make sure you set the properties of the sound as
desired (in my case, I’ll be using a SoundEffect so I need to specify
it in the Content Processor field):
All that remains is to add the xnb file to the Content folder of your
Monogame project.

Related

The type or namespace name "..." could not be found - Unity Vuforia

I am pretty new to Unity and Vuforia. I am trying to do solve this following problem. I have a script file that I am using for 2 different Image targets. Only 1 script should be running at a time. So i opened "DefaultObserverEventHandler.cs" to handle tracking events. I want to disable the script on that particular Image target when the tracking is lost. But when i try to get a reference on the script "QuizzBehaviour" i get an error. "QuizzBehaviour" is located in my Assets folder. Thanks in advance.
Ok, I fixed it. I made my own "DefaultObserverEventHandler.cs" script and placed it in the same folder where my "QuizzBehaviour.cs" script is.
"Find" will search through objects in scene not in your folders.However, at all costs you should avoid searching for objects by typing their name manually.In your case, the matter is quite simple. You can either add a script to an object and then use the get component command, or you can make an abstract class and refer to it.

Where in a project does Unity associate Image names with their source files?

I'm looking at the source code for a Unity project I just started collaborating on. I can't find where in the project Unity associates a filename with an Image object referred to in the C# scripts. (I'm completely new to Unity)
The game runs fine, so I know Unity is able to load the images, and there are multiple places in the C# scripts that refer to specific Images, but I can't find any place in the scripts that define what file an Image is loaded from (something like public Image sprite1 = loadImage("art.png");).
These sprites were imported through the Unity editor, so I'm not sure if Unity associated these Images with filenames somewhere in the project itself, in some non-human-readable file, or in Unity's program files (ie, outside of version control).
My main concern is that any changes I make to these sprites won't be reflected in version control, since there may be some crucial relationships defined outside of any tracked files.
Image references are generally handled by the drag and drop system, objects with scripts on them either in the scene view or in prefabs will have references to things like images set up via the Unity GUI.
Try using the searchbar and typing in the exact name to your image, from this should pull all gameobjects in your scene and all prefabs in your project that reference it.
Some extra background on how this works: (you don't need to know this, but it's handy)
Unity parses all content files it detects and assigns them a .meta file which contains a unique GUID that unity uses to track and reference that particular asset. Other meta files associated with prefabs and scenes will have that guid internally to keep a reference to that particular asset. This makes it vitally important to make sure you check in changed .meta files! They aren't generated local files, they're extremely important!
Unity Plugins for handling version control in unity should automatically check out files that change those references, generally it's pretty foolproof. Doing something as simple as renaming or moving a file will preserve the internal guid, so it's totally safe (just remember to rename and move files inside the unity editor! You can relocate files externally, just make sure you don't lose the .meta file)
Unity references images by means of GUID, that is assigned to an imported file on import, and saved to an invisible .meta file. AS long as you change both image filename and its corresponding .meta filename, and keep the GUID, unity will keep the reference, regardless of the actual file name (as long as you keep the GUID).
Important takeway is that you should never loose your .meta files (make sure they are tracked by your version control), as if you do, unity will generate a new random GUID for that file, and the relationship will never be restored back, which can be pretty catastrophic for a project.
Of course this does not apply to the images loaded in runtime, referenced by filenames.

EditorWindow implementation needs reimport to load asset

i currently try to extend the unity editor with custom EditorWindow implementations. It follows loosely this guide.
I try to save a asset containing serialized objects as a sort of database.
That works just fine!
However. If i close unity and reopen it
db = (FigureDB)AssetDatabase.LoadAssetAtPath("Assets/Logic/Database/FigureDB.asset", typeof(FigureDB));
doesnt load the asset file unless i manually reimport the EditorWindow implementation within unity (rightclick on cs-file -> reimport).
I code in Visual Studio (whether this matters or not.... i suspected a line-endings-problem, but it doesnt seem to be the case)
After reopening Unity i also see within the inspector (while selecting the asset) The associated script can not be loaded. Please fix any compile errors and assign a valid script (even though there are no compile time errors visible)
Any suggestions? If more info is necessary i will gladly provide them, however it doesnt seem to be a code problem but rather an IDE-problem.
Do you had use this?
AssetDatabase.ImportAsset("Assets/Logic/Database/FigureDB.asset", ImportAssetOptions.Default);

Load Raw Content from Unknown Directory

In my current game, modding is a HUGE part of the game. It took me a while to develop a good system of loading modded content into the game, but I finally settled on a method, and I would like to keep it if possible.
How I'm handling modded content
In the game's content folder (C:/Users/username/4X), there is a Mods folder. Each mod will have it's own folder inside of the Mod directory. The game goes through a file (Rather, it will. I haven't implemented it yet), and figures out which Mod directories that it's going to load. After it's figured that out, it loads all of the content into the game (I can explain in more detail if its pertinent to the topic, I just don't wan't to use up space on unnecessary things).
So what's your problem?
Well, the mods will all have raw resources (.fbx, .wav, .mp3), and since I can't load anything but XNB files, I have absolutely no idea how to load the mod's content. Well, I take it back, I've been thinking of a few solutions but I really don't know which is more practical, or if there is a better way of doing this.
First, I thought about borrowing some code from the Pipeline application, and building all of the mod's content the first time it was loaded, but I don't fully understand the Pipeline's code, and I didn't want to mess up something by partially implementing it.
Next, I thought about requiring mod creators to use the Pipeline to build their content before they release their mods, but that seemed kind of unprofessional, since I want to have a Mod Creation Engine that has all of the tools bundled together. Which brings me back to using some of the Pipeline's code and embedding it in the engine, but then I have the same issue as my last idea.
And finally, I thought about just loading raw content. But there's an obvious flaw with that idea.
So I guess what I'm asking is:
How can I load raw content? That is, content that hasn't been built into XNB Files
If not, how can I start learning about the code that makes the Pipeline application tick, since I'll probably have to use some of it's code?
You can actually load some raw content directly with MonoGame. For example:
var texture = Content.Load<Texture2D>("raw.png")`
should work if you've got the PNG file in the Content directory and set it's properties to Content / Copy if newer.
However, please note that this will only work for some content and some platforms. I know PNG files work, WAV files work (but they must be mono 44khz I believe) and I'm pretty sure I got MP3 files to work once.
You'll probably run into issues with FBX files and sprite fonts. There's nothing stopping you from bypassing the content manager and loading these yourself though. I've done this before with great success.
Oh, and the other thing I should mention, take a look at the TitleContainer.OpenStream method. This is a way to read a file without writing the platform specific code yourself.

Windows Form Application : Publish issue

I'm very new to Visual Studio 2010. I decided to create a simple WFA with C#.
Everything work fine with Images and Audio playback. My intention is to create a standalone application and send it to my friend as a gift. Problem I facing now is that when I tried to publish the application, the Images / Audio is still using the same location in my PC. So it won't play nor display any images.
Any comment on this or guide for me? I did try search for solution but doesn't seems to have any luck.
This is the code that I used to play Audio :
SoundPlayer ply = new SoundPlayer(#"C:\Users\Liam619\Documents\Visual Studio 2010\Projects\BirthdayApp\BirthdayApp\Resources\BirthdaySong.wav");
If I remove or change the path, the Application will hit error for locating the Audio file.
There may be several solutions to your problem.
a) embed the sound into a resource. As a beginner, resources may be a bit tricky to get it right the first time. But I want to encourage you reading something about it. You'll need resources when you want to translate your first program.
b) create an installer which copies the sound file to the installation directory. Try InnoSetup. If you're a programmer, sooner or later, you'll need to create a Setup anyway. Always worth knowing how to do that.
In that case, you still need the path to the sound file, but if you install your EXE into the same path as the sound file, see getting the application's executable directory.
everything in the database whether images or audio refers to your own server database.you have to send the database too with the app and the correct version .NET framework needs to be installed on the target PC.

Categories