how to restart game using ui button unity 4.6? - unity3d

I want that the player could play again when they're done playing.
The method works fine when I place it OnGUI(), but when I apply it to an UI button, the restart just hangs and the countdown timer isn't working at all.
public void playAgain(){
Application.LoadLevel (Application.loadedLevelName);
Character.score = 0;
time.second = 30.0f;
}

I believe that you probably set the time.timeScale = 0 when the game ended, and you probably forgot to start it over on the start of the level.

Related

Audio volume change is lagging

Im playing two tracks in sync. This works well, they sound like one song.
What im trying to do is allow player to switch one track on and of by toggling mute or setting volume to 0 or 1. The muted track continues playing in sync with other track but cant be heard.
But the volume change /muting is lagging. So even if you switch the sound on exactly on beat you wont hear that beat cause it takes a split second for the code to react. There must be a way to make the change instant?
Below is a part of the music manager that handles this. The full code also includes how sound is loaded and starts playing and can be seen here:
https://github.com/Lautaro/AudioTest/blob/master/Assets/MusicManager.cs
// Update is called once per frame
void Update()
{
if (Input.anyKeyDown)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Toggle();
}
}
}
void Toggle()
{
var clip = Sources.First(s => s.clip.name == "ExtraBeat");
// Same result if setting clip.volume instead of mute
clip.mute = !clip.mute;
}
The complete project with audio tracks can be cloned here. Use space to toggle sound.
https://github.com/Lautaro/AudioTest.git

Unity2d: return to previous application on button click

Is there a way of returning back to my previous game application when I close unity/close game application on button press. Basically I have a scene with a button in it, if the player press the button, it will bring them to a new level (scene 1). This is where I want to save this scene (scene 2) using something like playerprefs to keep tabs on it so that if I close the game application, or close unity and then I re-open and play the application (regardless of what scene I am in) the game should automatically bring me back to scene 2. Is it possible to return to previous application (even if I close the application or game) on button click?
So if the button is clicked on, and I exit out of the application and or game, then re open it and play the game then it should automatically bring me back to the scene I exited out of (scene 2)
When ever application quit event is called, Save current scene as player preference.
using UnityEngine.SceneManagement;
void quitGame()
{
Scene currentScene = SceneManager.GetActiveScene();
PlayerPrefs.SetString("lastSceneName",currentScene.name);
Application.Quit();
}
above code will save the name of current scene at time of application quit.
Now when you restart your game and your scene(0) is loaded. call following method in Start() or Awake() function.
void loadLastScene()
{
if (PlayerPrefs.GetInt("isFirstTime") == 0) //this is to make sure this chunk of code doen't run on the very first usage.
{
PlayerPrefs.SetInt("isFirstTime", 1);
}
else if (PlayerPrefs.GetInt("isFirstTime") == 1)
{
SceneManager.LoadScene(PlayerPrefs.GetString("lastSceneName"));
}
}
Code not tested.

Swift game won't stay paused

I'm having difficulty pausing the game when I leave and switch back to it.
I'm trying to pause the SKSpriteNode called main, which contains all my sprites, when the view returns from the background. In-game, I can touch the pause button and the game pauses, and the resume button and it resumes.
This is my code:
func didBecomeActive() {
println("didBecomeActive")
main.paused = true
}
The first time this runs is when the app opens for the first time, and everything is paused as it should be. The second time, is when it returns from the background, and suddenly all the animations (SKActions, particles, etc.) start working.
I've confirmed that the method is running, and I've also tried setting main.paused to false and then true, and even self.paused to true. Nothing works.
I'm completely stumped. Anyone know what the issue is here?
Setting self.scene.paused = YES should fix this. I have tried it with a game I am developing and it works fine.
Just set self.scene.paused = YES when the game enters the background, then when it return to the foreground, it should stay paused till you resume it, i.e. set self.scene.paused = NO.

How do you make an AuGraph restart a audio file from the start of the file?

I am working with Apple's MixerHost application. It is a great example of how to set up an AuGraph, however the stop button is really a pause button. When you hit play it continues playing the sound files from the last position. I want to have a true stop button that causes the play head to move back to the start. I looked up AuGraph in apple's docs but I do not see anything about starting the song over without having to go through the process if creating a brand new graph, and I would prefer not to have to do this since i have a large loading delay.
Figured it out. This was my method
for (int audioFile = 0; audioFile < NUM_FILES; ++audioFile) {
soundStructArray[audioFile].sampleNumber = 0;
}

Music not stopping when moving an app to the background

I am optimizing a game so that when there is an incoming call or if it is moved to the background for any other reason, it should stop the music, timers and pause correctly.
It works great other than the fact that for some reason the music doesn't stop playing even though I issued a command for it to stop. What's weirder is that when the game returns to the foreground there's 2 background musics playing instead of one.
Here is my some of my code, nothing too complicated or out of the ordinary:
...
NativeApplication.nativeApplication.systemIdleMode = SystemIdleMode.KEEP_AWAKE;
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleActivate);
NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleDeactivate);
...
private function handleActivate(event:Event):void
{
stage.frameRate = previousFrameRate;
//if the music was on when the game is moved to the background, then replay it when its moved back to the foreground
if(musicOn)
{
BGMusic.play();
}
}
private function handleDeactivate(event:Event):void
{
BGMusic.stop();
stage.frameRate = 0;
}
It is worth noting that if I don't replay the music when then game returns to the foreground (i.e. when I don't use BGMuusic.play() in handleActivate), there won't be any music as expected. It is only when I stop the music AND resume it later when moving to the foreground that the background music doesn't stop and I get two playing tracks.
Any ideas? As I said, everything else works fine, and the game correctly pauses. I am testing this on Flash builder Emulator since I don't have the necessarily certificates to test it directly on the iphone. So maybe it is a problem with the emulator itself rather than the code.
Thanks in advance
EDIT: Am writing this with flash and adobe air
Well that was stupid of me. Everything is working fine I just forgot to add another condition to the if statement. So instead of:
if(musicOn)
{
BGMusic.play();
}
It should be
if(musicOn && !BGMusic.isPlaying())
{
BGMusic.play();
}
Everything works fine now...I will change the variable name of musicOn to something clearer like musicButtonOn.
Anyways thanks for help. If you have any tips or comments feel free to tell me =D
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
NSLog(#"Playing stoped");
[player stop];
}