Continues to play audio in different scenes - c#

In my Unity Project, there are different menus, and each menu is a different scene. Each scene has button for controlling the music.
public void musicToggle(GameObject Off){
if (PlayerPrefs.GetInt ("Music") == 1) {
musicPlayer.Stop ();
Debug.Log ("Stop 2");
PlayerPrefs.SetInt ("Music", 0);
Off.SetActive(true);
}
else {
musicPlayer.Play ();
Debug.Log ("Play 2");
PlayerPrefs.SetInt ("Music", 1);
Off.SetActive (false);
}
}
This is my musicToogle function. In every scene the music restarts, and in every scene when I want to turn on/off the music, I click a button which deploys this code. However, I don't want the music to restart every scene change, I want it to resume, and I want to be able to control the music(turn on/off) in every scene. How can I do this ?

My answer assumes that musicPlayer variable is a type of AudioSource.
There are really two ways to do this:
1.Instead of using musicPlayer.Stop to stop the music,
Use musicPlayer.Pause(); to pause then music then use musicPlayer.UnPause(); to un-pause it. This will make sure that the music resumes instead of restarting it.
In this case, you use DontDestroyOnLoad(gameObject); on each GameObject with the AudioSource so that they are not destroyed when you are on the next scene.
2.Store the AudioSource.time value. Load it next time then apply it to the AudioSource before playing it.
You can use PlayerPrefs to do this but I prefer to store with json and the DataSaver class.
Save:
AudioSource musicPlayer;
AudioStatus aStat = new AudioStatus(musicPlayer.time);
//Save data from AudioStatus to a file named audioStat_1
DataSaver.saveData(aStat, "audioStat_1");
Load:
AudioSource musicPlayer;
AudioStatus loadedData = DataSaver.loadData<AudioStatus>("audioStat_1");
if (loadedData == null)
{
return;
}
musicPlayer.time = loadedData.audioTime;
musicPlayer.Play();
Your AudioStatus class:
[Serializable]
public class AudioStatus
{
public float audioTime;
public AudioStatus(float currentTime)
{
audioTime = currentTime;
}
}

You will need to create a singleton class and instantiate it on your first scene, then you can pass it around your various scenes as described in this unity answer: http://answers.unity3d.com/answers/12176/view.html

Related

How to preserve the full state of objects between scenes?

When loading a new Scene I run into the trouble of having my mouse drag not carry over to the next scene and having to re-click when the new scene is loaded.
I would like the mouse click to carry over seamlessly to the next scene without the player noticing and more generally I would like to know how best to preserve certain game objects and make them carry over to the next scene.
In essence, what I'm trying to do is have the entire game act like one big scene that the player can play trough but still be broken down into smaller scenes that could be accessed or transformed into levels at a later stage.
Thanks in advance.
This is the code I'm currently using
using UnityEngine;
using System.Collections;
using UnityEngine.Profiling;
public class MoveBall : MonoBehaviour
{
public static Vector2 mousePos = new Vector2();
private void OnMouseDrag()
{
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
transform.position = mousePos;
DontDestroyOnLoad(this.gameObject);
}
}
Bellow is the script that is responsible for the loading of the scene:
public class StarCollision : MonoBehaviour
{
private bool alreadyScored = false;
private void OnEnable()
{
alreadyScored = false;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("White Ball"))
{
if (!alreadyScored)
{
ScoreScript.scoreValue += 1;
StartCoroutine(ChangeColor());
alreadyScored = true;
}
}
if (ScoreScript.scoreValue > 4)
{
SceneManager.LoadScene(1);
}
}
private IEnumerator ChangeColor()
{
ScoreScript.score.color = Color.yellow;
yield return new WaitForSeconds(0.1f);
ScoreScript.score.color = Color.white;
gameObject.SetActive(false);
}
}
I think the main reason why it doesn't work is that you probably also have another Camera in the new Scene.
The OnMouseDrag rely on the Physics system internally using the objects Collider and raycasts from the Camera. Now if you switch Scene I'ld guess the one Camera gets disabled so your drag gets interrupted.
Also using LoadScene instead of LoadSceneAsync causes a visible lag and might also be related to the issue.
I have a maybe a bit more complex solution but that is what I usually do:
1. Have one Global Scene "MainScene"
This Scene contains stuff like e.g. the MainCamera, global ligthning, global manager components that should never be destroyed anyway.
2. Use additive async Scene loading
You said you do not want your user to not note when the scene switches so I would recommend using SceneManager.LoadSceneAsync anyway.
Then in order to not unload the before mentioned MainScene you pass the optional parameter LoadSceneMode.Additive. This makes the new Scene be loaded additional to the already present one. Then later you only have to exchange those by unloading the previously additive loaded scene.
I created a very simple static manager for this:
public static class MySceneManager
{
// store build index of last loaded scene
// in order to unload it later
private static int lastLoadedScene = -1;
public static void LoadScene(int index, MonoBehaviour caller)
{
caller.StartCoroutine(loadNextScene(index));
}
// we need this to be a Coroutine (see link below)
// in order to correctly set the SceneManager.SetActiveScene(newScene);
// after the scene has finished loading. So the Coroutine is required
// in order to wait with it until the reight moment
private static IEnumerator loadNextScene(int index)
{
// start loading the new scene async and additive
var _async = SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
// optionally prevent the scene from being loaded instantly but e.g.
// display a loading progress
// (in your case not but for general purpose I added it)
_async.allowSceneActivation = false;
while (_async.progress < 0.9f)
{
// e.g. show progress of loading
// yield in a Coroutine means
// "pause" the execution here, render this frame
// and continue from here in the next frame
yield return null;
}
_async.allowSceneActivation = true;
// loads the remaining 10%
// (meaning it runs all the Awake and OnEnable etc methods)
while (!_async.isDone)
{
yield return null;
}
// at this moment the new Scene is supposed to be fully loaded
// Get the new scene
var newScene = SceneManager.GetSceneByBuildIndex(index);
// would return false if something went wrong during loading the scene
if (!newScene.IsValid()) yield break;
// Set the new scene active
// we need this later in order to place objects back into the correct scene
// if we do not want them to be DontDestroyOnLoad anymore
// (see explanation in SetDontDestroyOnLoad)
SceneManager.SetActiveScene(newScene);
// Unload the last loaded scene
if (lastLoadedScene >= 0) SceneManager.UnloadSceneAsync(lastLoadedScene);
// update the stored index
lastLoadedScene = index;
}
}
This MySceneManager is a static class so it is not attached to any GameObject or Scene but simply "lives" in the Assets. You can now call it from anywhere using
MySceneManager.LoadScene(someIndex, theMonoBehaviourCallingIt);
The second parameter of type MonoBehaviour (so basically your scripts) is required because someone has to be responsible for running the IEnumerator Coroutine which can't be done by the static class itself.
3. DontDestroyOnLoad
Currently you are adding any GameObject you dragged at any time to DontDestroyOnLoad. But you never undo this so anything you touched meanwhile will be carried on from that moment ... forever.
I would rather use e.g. something like
public static class GameObjectExtensions
{
public static void SetDontDestroyOnLoad(this GameObject gameObject, bool value)
{
if (value)
{
// Note in general if DontDestroyOnLoad is called on a child object
// the call basically bubbles up until the root object in the Scene
// and makes this entire root tree DontDestroyOnLoad
// so you might consider if you call this on a child object to first do
//gameObject.transform.SetParent(null);
UnityEngine.Object.DontDestroyOnLoad(gameObject);
}
else
{
// add a new temporal GameObject to the active scene
// therefore we needed to make sure before to set the
// SceneManager.activeScene correctly
var newGO = new GameObject();
// This moves the gameObject out of the DontdestroyOnLoad Scene
// back into the currently active scene
gameObject.transform.SetParent(newGO.transform, true);
// remove its parent and set it back to the root in the
// scene hierachy
gameObject.transform.SetParent(null, true);
// remove the temporal newGO GameObject
UnityEngine.Object.Destroy(newGO);
}
}
}
This is an Extension Method which allows you to simply call
someGameObject.SetDontDestroyOnLoad(boolvalue);
on any GameObject reference.
Then I changed your script to
public class MoveBall : MonoBehaviour
{
public static Vector2 mousePos = new Vector2();
// On mouse down enable DontDestroyOnLoad
private void OnMouseDown()
{
gameObject.SetDontDestroyOnLoad(true);
}
// Do your dragging part here
private void OnMouseDrag()
{
// NOTE: Your script didn't work for me
// in ScreenToWorldPoint you have to pass in a Vector3
// where the Z value equals the distance to the
// camera/display plane
mousePos = Camera.main.ScreenToWorldPoint(new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
transform.position.z)));
transform.position = mousePos;
}
// On mouse up disable DontDestroyOnLoad
private void OnMouseUp()
{
gameObject.SetDontDestroyOnLoad(false);
}
}
And in your StarCollision script you only have to exchange
SceneManager.LoadScene(1);
with
MySceneManager.LoadScene(2, this);
Demo
For a little demonstration I "faked" it using two simple scripts
This one in the Main scene
public class LoadFirstscene : MonoBehaviour
{
// Start is called before the first frame update
private void Start()
{
MySceneManager.LoadScene(1, this);
}
}
And this one in the other scenes
public class LoadNextScene : MonoBehaviour
{
[SerializeField] private int nexSceneIndex;
private void Update()
{
if (!Input.GetKeyDown(KeyCode.Space)) return;
MySceneManager.LoadScene(nexSceneIndex, this);
}
}
And have 3 Scenes:
Main: As mentioned contains
the MainCamera
a DirectionalLight
the LoadFirstScene
test: contains
a MoveBall "Sphere"
the LoadNextScene
test2: contains
a MoveBall "Cube"
the LoadNextScene
With the indexes matching the build settings (make sure Main is always at 0 ;) )
I can now switch between test and test2 using the Space key.
If I drag one of the objects meanwhile I can carry it on into the next scene (but only 1 at a time). I can even take it on again back to the first scene in order to have e.g. two sphere objects I can play with ;)

How to resume game after pressing back in menu

Hey I am trying to make a pause menu in my game. When escape is pressed pause game go to menu, but now I want to be able to press back in menu and resume my game. So far I can only pause game and cant press back. Also if i press Play in menu it starts at my tutorial scene and not the current scene. Is there a smart way to do this? Without resetting my game.
`using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class MenuScript : MonoBehaviour
{
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Escape))
{
if (Time.timeScale == 0)
{
Time.timeScale = 1;
}
else
{
Time.timeScale = 0;
}
SceneManager.LoadScene("Menu");
}
}
}`
I get that this isn't straightforward problem since you seem new to Unity.
You got the idea correctly, changing the time scale will freeze all agents on the scene. HOWEVER, if you load a new scene you'll need to reload the game scene - losing any data you had (and that is not what you want). My advice is creating an overlay element (UI) on the game scene and just show/hide it. There are multiple tutorials online use this as an starting point. Let me know if you require more help.
code sample
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.Escape))
{
if (Time.timeScale == 0)
{
Time.timeScale = 1;
pauseMenu.gameObject.setActive(true);
}
else
{
Time.timeScale = 0;
pauseMenu.gameObject.setActive(false);
}
}
}
You will need a reference to the pauseMenu game object attached on this script using the Unity editor.
Depending on what you need, this would work as well.
private bool _isPaused;
private void Update(){
if (Input.GetKeyUp(KeyCode.Escape))
{
_isPaused = !_isPaused;
if (_isPaused)
{
//Do Pause Logic here
}
else
{
//Do Unpause Logic Here
}
}
}

How to fix "Can't Play a Disabled Audio Source"?

I am trying to add a sound into my game that whenever the player moves over a certain space it plays a crunch sound. I have created the AudioSource file and a .OGG file for the sound.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpaceBlue : MonoBehaviour
{
public Transform spaceNext;
public AudioSource stepOnObject;
public AudioClip stepOnSound;
private void Start()
{
stepOnObject.clip = stepOnSound;
stepOnObject.enabled = true;
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
stepOnObject.Play();
if (BoardScript.diceValue > 0)
{
BoardScript.diceValue -= 1;
Debug.Log("The dice are now " +BoardScript.diceValue);
other.transform.LookAt(spaceNext);
}
}
}
}
I have included the source and clip to my game object and i have tried it both with and without "Play on wake" selected.
Whenever the play walks over the player walks over the object i get a warning in the unity engine saying that the source is disabled.
Any help is appreciated :)
I had a similar problem and I fixed it by changing the location of the Play() call to after calling Destroy(GameObject). I would recommend trying moving the call to Play() to the end of the function, or trying Invoke("stepOnObject.Play", 0.5f); to ensure it gets called.
Otherwise, make sure its checkbox is ticked, and that the AudioSource actually has a AudioClip attached.
If you have any piece of code in some other script that Destroys this game object or makes SetActive false, then the best way to solve this problem will be to delay that piece of code by some time using a Coroutine.

Cannot generate sound when moving method to another class

So for starters, I wish to say that the code below works. But its not structured how I want it to:
public AudioClip pickup;
void OnTriggerEnter(Collider collider)
{
//Trigger for pickups
if (collider.gameObject.CompareTag("Pickup"))
{
AudioSource audio = GetComponent<AudioSource>();
audio.PlayOneShot(pickup);
}
}
But I want to split the class into two and have the player controller (part of it written above) be used to call a function from a new class called 'LevelClass' which will house the method for pickup like so:
public void collidePickup(Collider collider)
{
collider.gameObject.SetActive(false);
AudioSource audio = GetComponent<AudioSource>();
audio.PlayOneShot(pickup);
}
and have player controller call the function like so:
level.collidePickup(collider);
I also linked the sound file with the script so that it would have a sound to play on. From testing, I determined that the problem is being caused in the AudioSource section as its not finding an audio source to work with. Is there something i can do so that i would be able to use an instance of audiosource within the levelclass rather than in the playercontroller?
You should be able to pass a reference to your audio source into the function like this.
public void collidePickup(Collider collider,ref AudioSource audio)
{
collider.gameObject.SetActive(false)
audio.PlayOneShot(pickup);
}
Then you just need to pass the AudioSource to he function when you call it.
You should know, that every AudioSource emits sound according to it's position and uses Doppler effect parameters with it's rigidbody relative velocity.
Also note that every AudioSource can play exactly one clip at a time.
One of the solutons is to create a new GameObject with AudioSource on it (either from prefab on the your audio manager object or from code) for every sound that you want to play in your game, and immediately Destroy it with timeout equal to audio length.
public void collidePickup(Collider collider)
{
collider.gameObject.SetActive(false);
PlaySoundAt(pickup, collider.transform.position);
}
private static void PlaySoundAt(AudioClip sound, Vector3 position)
{
var go = new GameObject("PickupSound");
go.transform.position = position;
var source = go.AddComponent<AudioSource>();
source.PlayOneShot(sound);
Destroy(source, sound.length);
}

Is it possible to call a function on Unity Program Start?

I was wondering if there was a way in Unity that when I start my program on a scene it fires a function first, I should add that I want this one function to work regardless of what scene I'm in. So a simple Start function wont cut it. Not sure if this is possible in Unity?
public void ProgramBegins()
{
//FIRES FIRST ON ANY SCENE
//DO STUFF
}
RuntimeInitializeOnLoadMethodAttribute can help you :)
using UnityEngine;
class MyClass
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void OnBeforeSceneLoadRuntimeMethod()
{
Debug.Log("Before scene loaded");
}
}
Here you can find the execution order of all functions in unity: http://docs.unity3d.com/Manual/ExecutionOrder.html
Awake is the first function to be executed in your standalone application. For it to run you need to have a GameObject with a attached script containing the Awake function. This should be added to every scene if you want it to run regardless of the scene.
You still have to decide what is the startup scene of your game. So it is enough to add the GameObject there, if you actually want it to run just in the start of the program.
I use a prefab _AppStartup which is just an empty game object having a script AppStartup. Drag this in every scene and configure AppStartup to be executed as first like #maZZZu has stated.
AppStartup performs the following jobs:
Global initialisation tasks when the app is started
Switch to boot scene if have start an arbitrary scene in editor mode
Scene specific initialisation tasks
public class AppStartup : MonoBehaviour
{
const int bootSceneNo = 0;
public static bool veryFirstCallInApp = true;
void Awake ()
{
if (veryFirstCallInApp) {
ProgramBegins ();
if (Application.loadedLevel != bootSceneNo) {
// not the right scene, load boot scene and CU later
Application.LoadLevel (bootSceneNo);
// return as this scene will be destroyed now
return;
} else {
// boot scene stuff goes here
}
} else {
// stuff that must not be done in very first initialisation but afterwards
}
InitialiseScene ();
veryFirstCallInApp = false;
DestroyObject (gameObject);
}
void ProgramBegins()
{
// code executed only once when the app is started
}
void InitialiseScene ()
{
// code to initialise scene
}
}
So all you have to do is drag this prefab in every scene manually and give it -100 or whatever in the script execution order. Especially when the project grows and relies on a predefined scene flow it will save you al lot time and hassle.
Yes...
Decorate your method with [RuntimeInitializeOnLoadMethod].
It will be invoked as soon as the scene has finished loading (after Awake events).
[RuntimeInitializeOnLoadMethod]
static void OnRuntimeMethodLoad() {
Debug.Log("After Scene is loaded and game is running");
}
Documentation: https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html
//try this it work in my case
public static bool isFirstLoad;
void Awake()
{
//your work that run only once even restart this scene
mainPanel.SetActive(!isFirstLoad);
if(!isFirstLoad)
{
isFirstLoad=true;
}
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(this.gameObject);
}
}

Categories