Toggle music in unity - c#

Currently I have my toggle to On Value Change make AudioSource.enabled, which does turn the music off.
However when I die in the game the level gets reloaded causing the toggle to go back to its default state and then i am unable to turn the music back on.
So what is the best way to make a music toggle and have the state of the toggle be remembered on load?

Another solution would be a singleton music manager game object that is created only once and is persistent between scenes, meaning that it will not be destroyed when you reload a scene or load a different scene.
You can read more about Singleton patterns here: http://unitypatterns.com/singletons/

You can save persistent data across scenes using PlayerPrefs. Just store there the audio status and be sure to check if it's enabled when loading a new scene (for example inside Awake method of a MonoBehaviour attached on the same AudioSource's GameObject).
Side note
Depending on the specific situations, turn off globally the volume could be easier using AudioListener.volume rather than modifying all AudioSources.

Related

Scene design (color) changes after loading scene

I'm trying to make an FPS game using unity, but I've come across a problem. When I load my main scene after pressing "RESTART", it somehow causes my game to change appearance. But when I start the game from the main scene, it looks perfectly fine.. I've tried disabling Post Processing but I believe that's not what's causing the issue, I've provided some pictures of what happens when I load the main scene through the GameOver scene and when I load it through the MainScene.
Scene loaded through the press of a button ------Main Scene (what it's supposed to look like)
Your help is much apreciated
Ok, so after quite a while of messing around with the settings I FINALLY figured it out. To get rid of uneven lighting between the scenes all you have to do is go to Window>Rendering>Lighting>Environment> (make sure "Auto Generate" is UNCHECKED) and click Generate lighting!!!

how to transfer a float from one scene to another

Hi I am making a fps shooter game with unity 3d and I am trying to add a in game currency system but in order to do this I need to transfer a float called coins that is on my players movement script from the level scene to the store scene in order for it to be spent and displayed on screen as a UI dose anybody know how I can do this thank you so much for your help😁
Do not store variables in scripts where they do not belong. There are multiple solutions to this problem:
Option is to create a ScriptableObject that contains variables that need to be transferred. It is instantiated in the editor by you and then assigned as an asset to your player prefab. Instead of setting a variable in the player itself, the player just sets a variable in this storage.
This storage can be anything, from a single variable (see this video for reference) to big systems like Unitys Tilemap System.
You create a component that is attached to a game object that resides in the "Dont Destroy On Load" scene. This can be done by creating a monobehaviour script and calling DontDestroyOnLoad(gameObject) in its Start method. Additionally, you can make it a singleton and access it from everywhere. For reference see this answer from the game dev stackexchange.
To be honest, I always try to go with the first one. If you want some variables to persist in a "game state" (that's what its called in Unreal), then you create it externally and reference it throughout you session. There are really a lot of situational approaches and there is no real right or wrong, but you should always keep in mind that a class should only serve a single purpose and a player script that moves the player and stores the current stats (that should live longer than the player itself) is usually a bad approach.

DontDestroyOnLoad - how to destroy?

I'm working on a Vuforia (legacy) application.
In "Play" mode in Unity I can see that some "DontDestroyOnLoad" content is being generated, (looks like it's sort of a camera).
This might be a reason of why I have some problems when switching between the scenes, so the question is how to make these elements "destroyable"?
'DontDestroyOnLoad' It means that they won't manage it automatically. so if you want to destroy it, you should destroy manually like
GameObject.Destroy(GameObject.Find("TextureBufferCamera"));

Mecanim Animation in Unity

I am facing a problem while playing a animation which is already playing at the moment.
e.g. I am currently working on boxing fighting game in which when I punch twice, first time animation is played but second time, it doesn't restart animation.
I also tried turning off that animation if it is already running, not finding any clue regarding this problem as I'm beginner in mecanim animation.
Mecanim doesn't allow you to do this directly. A state can only be in one state at a time. Either at the end and fading out or at the start and fading in, but not both at once.
You can do a hacky workaround where you make two states with the same animation and alternate between them each time you want to play that animation. It's annoying to set up and makes it take more effort if you need to refactor, but it does work.
My Animancer plugin on the Unity Asset Store has a function specifically for this situation: CrossFadeNew will always fade the animation in from the beginning. If it's already playing it will automatically create a new state to fade in while the old one fades out. Feel free to contact me via the support email address listed on the store page and in the user manual if you have any questions about it.

Monogame content use efficiency

I've been using Monogame for a awhile now and I was just wondering what is the best way to load my content? Lets say i have an intervals system that constantly creates objects on the screen, so should I load the object's sprite in the game class and put the sprite variable in the constructor or should I put the content variable in the object constructor and load the sprite from within the object?
Btw by best way I mean I try to keep the framerate and use less memory, thx in advance!
Best way for frame rate is to load it before game loop starts. But the best way for less memory is right when you need it and dispose of it after your done. Not necessarily mutually inclusive... pick your poison.
You have 3 different solutions, and each will fit a different type of game.
Load everything in LoadContent
This will cause a longer initial startup time, but after everything is loaded, you wont have to wait for anything anymore. However keep in mind that this is only suited for small games such as Tetris, Arcanoid or Chess. Games that generally doesn't have a lot of content. Keep in mind that the more content you load, the more memory it will consume.
Load everything needed for the current scene
This is what most games does, since you wont load data for scenes that you never use, and you wont load data for scenes you're not yet accessing.
Load everything needed for the current scene (Extended)
Like the previous answer, but with a small twist. If possible, load content while displaying other content. For instance, as soon as the player finished the last step for completing a scene, initiate the loading of the next scene.
Arcanoid example: As soon as the ball hits the last block (or if you're brave, even as soon as you can calculate that the ball WILL hit the final block), initiate the loading of the content for the next scene. Let this load while the ball flies towards the final block, and also while displaying the score for the current scene (Time, deaths, bonuses, etc.)
And should the player actually close the dialogues before the scene has been loaded, show another loading scene while the data finishes loading. That way, there might only be 1-2 seconds of loading time instead of 10.
Remember to perform all this loading in a background thread.

Categories