how to restart without time,.timescale effecting it
I have time.timescale = 0; turned on once the player dies. but if the player click restart, the game reloads. Why is the timescale still effecting the game when the game is restarting ><. how do I turn the timescale to normal again when player restart the game?
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag ("Player")) {
Destroy (other.gameObject);
Time.timeScale = 0;
Replay ();
}
}
this is how my player dies upon collision with an obstacle. When player dies, the game was still running and the player still obtaining point even though the player is not active anymore. That why I've decided to freeze the game and click restart button
Answer by Zen_Hap · May 31, 2016 at 01:24 PM
script start:
using UnityEngine;
using System.Collections;
public class Hurt : MonoBehaviour {
public int health = 1;
void Start () {
Time.timeScale = 1f;
}
void Update () {
if (health <= 0){
Die();
}
}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Deadly") {
health--;
}
}
void Die () {
Time.timeScale = 0f;
Application.LoadLevel("DeathScreen"); #This is the death scene.
}
}
:script end
Above code is called Hurt.cs, (Don't include script start: and :script end, they are just to show you what to copy) the code must be c#. Tag the player as Deadly and whatever kills the player as Deadly, then make sure they both have a collider set as trigger. If the thing that kills the player is a bullet, not another player or whatever, make sure the bullet has a trigger collider as well. Now if you save the script, add it to the thing that kills the player (e.g: bullet, zombie, bomb) and the player, once you have done that an option will pop up in the editor that says health, this is the health of the player from the public int in the script. Set the health to what you want but make sure that the health is the same on the player and the thing that kills the player. If it is set to three and you get shot in the game, the player takes three hits before he dies. Now make a new scene called DeathScreen . Once you have made it make a new js file with this script:
pragma strict
function OnMouseUp () { Application.LoadLevel("Game"); #This is the game scene. }
Add the js script to the Main Camera on the death screen. Once you have done that create a UI button (This is your restart button) and scroll down to On Click (), and click the plus icon. Drag the Main Camera into the slot in the inspector and select what you called the script. Now click play and test it, if you click restart it should take you to the game scene!
Now if you have done it right, it should work fine! Thanks!
(P.S: Please go on gamejolt and find my game called Buzzle, if you are on windows you will be able to download and play it! the death system works exactly the same way in Buzzle and the script I have shown you is the one that I made for Buzzle! Play Buzzle and tell me if you like it please! Look at how the death system runs in that and it should be fine note:(I forgot to add the event system for mega mode so the restart menu dosen't work from mega mode, but it works fine in regular mode! :P))
Thanks!
Answer by Quertie · May 31, 2016 at 12:52 PM
Just add:
void Start()
{
Time.timeScale = 1;
}
So that the game unfreezes when the scene is loaded! Hope this solves your issue :)
hey thanks quertie. I can't believe it was that simple and it took me forever to solve it
I also used: Time.timeScale = 1f; In the void start, I just wanted to tell you how to d the damage aswell :)
I'm currently updating Buzzle and it's definately WIP but here is the link to how it is so far (I hope that you like it :) ): http://www.gamejolt.com/games/buzzle/138631